Categories
React Answers

How to import image (.svg, .png) in a React component?

Spread the love

Sometimes, we want to import image (.svg, .png) in a React component.

In this article, we’ll look at how to import image (.svg, .png) in a React component.

How to import image (.svg, .png) in a React component?

To import image (.svg, .png) in a React component, we can import the image directly.

For instance, we write

import React from "react";
import image from "./img1.png";
import "./helloWorld.scss";

const HelloWorld = () => (
  <>
    <img src={image} alt="some image" />
  </>
);
export default HelloWorld;

to add the img element into the HelloWorld component.

We set its src prop to the image that we import with the import statement.

We can import it with import since the Webpack file loader plugin is included with Create React app projects.

Conclusion

To import image (.svg, .png) in a React component, we can import the image directly.

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 *