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

后端 未结 7 1233
一整个雨季
一整个雨季 2020-12-04 23:30

I am trying to import an image file in one of my react component. I have the project setup with web pack

Here\'s my code for the component

import Di         


        
相关标签:
7条回答
  • 2020-12-04 23:31

    Simple way is using location.origin
    it will return your domain
    ex
    http://localhost:8000
    https://yourdomain.com

    then concat with some string...
    Enjoy...

    <img src={ location.origin+"/images/robot.svg"} alt="robot"/>
    

    More images ?

    var images =[
    "img1.jpg",
    "img2.png",
    "img3.jpg",
    ]
    
    images.map( (image,index) => (
    
      <img key={index} 
           src={ location.origin+"/images/"+image} 
           alt="robot"
      />
    ) )
    
    0 讨论(0)
  • 2020-12-04 23:32
    import React, {Component} from 'react';
    import imagename from './imagename.png'; //image is in the current folder where the App.js exits
    
    
    class App extends React. Component{
        constructor(props){
         super(props)
         this.state={
          imagesrc=imagename // as it is imported
         }
       }
    
       render(){
           return (
    
            <ImageClass 
            src={this.state.imagesrc}
            />
           );
       }
    }
    
    class ImageClass extends React.Component{
        render(){
            return (
    
                <img src={this.props.src} height='200px' width='100px' />
            );
        }
    }
    
    export default App;
    
    0 讨论(0)
  • 2020-12-04 23:37

    try using

    import mainLogo from'./logoWhite.png';
    
    //then in the render function of Jsx insert the mainLogo variable
    
    class NavBar extends Component {
      render() {
        return (
          <nav className="nav" style={nbStyle}>
            <div className="container">
              //right below here
              <img  src={mainLogo} style={nbStyle.logo} alt="fireSpot"/>
            </div>
          </nav>
        );
      }
    }
    
    0 讨论(0)
  • 2020-12-04 23:37

    I also had a similar requirement where I need to import .png images. I have stored these images in public folder. So the following approach worked for me.

    <img src={process.env.PUBLIC_URL + './Images/image1.png'} alt="Image1"></img> 
    

    In addition to the above I have tried using require as well and it also worked for me. I have included the images inside the Images folder in src directory.

    <img src={require('./Images/image1.png')}  alt="Image1"/>
    
    0 讨论(0)
  • 2020-12-04 23:37

    Solved the problem, when moved the folder with the image in src folder. Then I turned to the image (project created through "create-react-app")
    let image = document.createElement("img"); image.src = require('../assets/police.png');

    0 讨论(0)
  • 2020-12-04 23:40

    You can use require as well to render images like

    //then in the render function of Jsx insert the mainLogo variable
    
    class NavBar extends Component {
      render() {
        return (
          <nav className="nav" style={nbStyle}>
            <div className="container">
              //right below here
              <img src={require('./logoWhite.png')} style={nbStyle.logo} alt="fireSpot"/>
            </div>
          </nav>
        );
      }
    }
    
    0 讨论(0)
提交回复
热议问题