./
means relative to the current file.
/
means it's using the project-defined root
path or baseUrl
which is the src
folder for react. So /
from any component would start from the src
folder.
../
goes up a directory from where the file is currently at and that likely would work here as well:
<img src="../icons/dashboard-sidebar-toggler.svg" className="mr-12"></img>
But I think the way you did it is cleaner:
<img src="/icons/dashboard-sidebar-toggler.svg" className="mr-12"></img>
Additionally, you can import images at the top of your react file as well and use the import as a source:
import sideBarTogglerSVG from '/icons/dashboard-sidebar-toggler.svg';
<img src={sideBarTogglerSVG} className="mr-12" />
EDIT
Here is a basic example of possible and not possible situations for importing images in a React app.
import React from "react";
import cat2 from "./assets/cat2.jpeg"; // <--- "relative" from `src`
import kitten from "./assets/kitten.jpg"; // <--- "relative" from `src`
// import checkSVG from "/assets/svgs/check.svg"; // <--- "relative" from `package.json`, won't work without module aliasing
// import checkSVG from "assets/svgs/check.svg"; // <--- treated as a `node_module` import, won't work
import checkSVG from "./assets/svgs/check.svg"; // <--- "relative" from `src`
export default function App() {
return (
<div>
<img width="40" height="40" src={checkSVG} />
{/* Looking inside /public */}
<img alt="Blue eyed cat" src="/cat.jpg" />
<img alt="Sleeping cat" src={cat2} />
{/* Looking inside /public */}
<img alt="Kitten Not Showing" src="./kitten.jpg" />
<img alt="Kitten Showing" src={kitten} />
</div>
);
}
Here's a working sandbox to try out React image importing.