Categories
React Answers

How to set the correct path for img on React.js and JavaScript?

Spread the love

Sometimes, we want to set the correct path for img on React.js and JavaScript.

In this article, we’ll look at how to set the correct path for img on React.js and JavaScript.

How to set the correct path for img on React.js and JavaScript?

To set the correct path for img on React.js and JavaScript, we can import the image to get the right path.

For instance, we write

import logo from "./logo.png";

class Nav extends Component {
  render() {
    return <img src={logo} alt={"logo"} />;
  }
}

to import the logo.png with import.

And then we set the img element’s src attribute to logo to load the image.

This works with Create React app projects since it has the image loader built in that lets us import images with import.

Conclusion

To set the correct path for img on React.js and JavaScript, we can import the image to get the right path.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *