Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

trying to import an image in react

I’m just trying to import a png onto a navbar but I keep getting errors.

import React from "react";
import { Nav, NavLink, Bars, NavMenu } from "./navBarComponent";
import logo-light from "../logo-light.png";

const Navbar = () => {
return (
 <>
<Nav>
 <NavLink to="/">
<img src={"logo-light"} alt="logo" />
</NavLink>
<Bars />
<NavMenu>
<NavLink to="/ourcompany">Our Company</NavLink>
<NavLink to="/contact">Contact</NavLink>
<NavLink to="/locations">Locations</NavLink>
</NavMenu>
</Nav>
 </>
);
};

export default Navbar;

in vs code the "import logo-light from" part has red error lines under it. when I hover it says ‘import… =’ can only be used in typescript files.
in the console it says:

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I’ve tried moving the image to public, renaming the image, etc but nothing seems to work. what am I doing wrong?

>Solution :

You don’t have to write the same name as the filename You can use an alias name you want and import that directly in the src like below

   
import logolight from "../logo-light.png";

<img src={logolight} alt="logo" />
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading