admin管理员组文章数量:1394751
I am not understanding how import works in React. I'm currently using create-react-app
. I have a folder inside ponents folder named images
:
- src
- ponents
- images
Insides the ponents folder I also have a js file named ProductSquare.js
.
I am trying to import a picture from images folder using this line
import bumper from './images/bumper1.png';
I have tried changing it to only bumper1.png
and many other options. What am I doing wrong here? ( would appreciate a better way to do it in the future).
Is this how programmers import pictures in the real world?
I am not understanding how import works in React. I'm currently using create-react-app
. I have a folder inside ponents folder named images
:
- src
- ponents
- images
Insides the ponents folder I also have a js file named ProductSquare.js
.
I am trying to import a picture from images folder using this line
import bumper from './images/bumper1.png';
I have tried changing it to only bumper1.png
and many other options. What am I doing wrong here? ( would appreciate a better way to do it in the future).
Is this how programmers import pictures in the real world?
Share Improve this question edited Jun 13, 2018 at 19:27 ischenkodv 4,2552 gold badges29 silver badges37 bronze badges asked Jun 13, 2018 at 19:19 user9878643user9878643 3184 silver badges15 bronze badges 1- 1 possible duplicate: stackoverflow./questions/43823289/… – Ethan Ryan Commented Jun 13, 2018 at 19:24
4 Answers
Reset to default 2Try using <img src={require('./images/bumper1.png')} />
P.S. The similar question goes here
If you are using something like Webpack (which create-react-app
does) then yes, you can just import the images (and most other file types). They end up getting converted into something (I believe images end up as a data URL).
If your folder structure looks like this:
- src
- ponents
- ProductSquare.js
- images
Then you should be able to use the image like this:
// in ProductSquare.js
import React from 'react';
import bumper from './images/bumper1.png';
class ProductSquare extends React.Component {
render() {
return <img src={ bumper } />;
}
}
If it isn't, double-check that all of the files are named what you think they should be (check for typos and what not).
If everything is correct and it isn't working, try just adding console.log(bumper)
before the class. Then:
- if it outputs a string that looks like
data:blahblah
it should work - if it outputs undefined, the image may not be named properly
- if it doesn't output, you might not be using
ProductSquare
correctly
Using the public
Folder
Note: this feature is available with
[email protected]
and higher.
Create a folder in public folder, for example, 'image', add your photos there.
After that wherever you want you can use image address like this:
<img className="img1" src="/image/pic.jpg" alt=""/>
You Need to use .default after require
<img src={require('./images/bumper1.png').default} />
本文标签: javascriptimporting images locally in reactStack Overflow
版权声明:本文标题:javascript - importing images locally in react - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744102790a2590941.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论