"; 2. Through "import zzsc from "./ Use the zzsc1.png "" method to introduce images, and then directly introduce variables into img."/> "; 2. Through "import zzsc from "./ Use the zzsc1.png "" method to introduce images, and then directly introduce variables into img.">

Home>Article>Web Front-end> What should I do if the imported react image does not display?

What should I do if the imported react image does not display?

藏色散人
藏色散人 Original
2023-01-03 15:30:58 3379browse

Solution to importing non-displayed images in react: 1. Import images through "What should I do if the imported react image does not display?"; 2. Import images through "import zzsc from "./zzsc1.png"", and then directly introduce variables in img.

What should I do if the imported react image does not display?

The operating environment of this tutorial: Windows 10 system, react18.0.0 version, Dell G3 computer.

What should I do if the imported image in react does not display?

The problem of introducing image path in react is correct but not displayed on the page or cannot be displayed normally after packaging

1. How to introduce images in react

In the past, we usedimgto introduce images as follows. Writing this way in react will cause an error:

What should I do if the imported react image does not display?

I summarized it below Two ways:

1. Use the require method to introduce Lu Jin. Wrong usage:

What should I do if the imported react image does not display?

The above will not take effect, because if you use create-react-app and require to import images, require returns an ES module instead of a string. This is because in file-loader, the esModule option is enabled by default.

So use one of the following methods to import the image: add .default after it

What should I do if the imported react image does not display?

2,Introduce it through !important

import zzsc from "./zzsc1.png"

Then in Just introduce the variable directly intoimg. The name of this variable can be chosen arbitrarily.

What should I do if the imported react image does not display?

The rendering is as follows:

What should I do if the imported react image does not display?

What should I do if the imported react image does not display?

However, sometimes the picture of your development environment is displayed, but when you deploy it to the test environment after packaging, you will find that the picture is gone again, scratching your head and wondering where the problem lies. . It's actually very simple. Webpack can only retrieve images in your public folder when packaging, so when placing image materials, the location of the image is also very important! Just put them all under the public file. Above code:

What should I do if the imported react image does not display?

If you add "./" in front of your package path, you need to write all the paths. It will be easier if you don't add it

Recommended study: "react Video tutorial

The above is the detailed content of What should I do if the imported react image does not display?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn