Load local images in React.js

后端 未结 10 2040
隐瞒了意图╮
隐瞒了意图╮ 2020-11-30 23:08

I have installed React using create-react-app. It installed fine, but I am trying to load an image in one of my components (Header.js, file path: <

相关标签:
10条回答
  • 2020-11-30 23:26

    In React or any Javascript modules that internally use Webpack, if the src attribute value of img is given as a path in string format as given below

    e.g. <img src={'/src/images/logo.png'} /> or <img src='/src/images/logo.png' />
    

    then during build, the final HTML page built contains src='/src/images/logo.png'. This path is not read during build time, but is read during rendering in browser. At the rendering time, if the logo.png is not found in the /src/images directory, then the image would not render. If you open the console in browser, you can see the 404 error for the image. I believe you meant to use ./src directory instead of /src directory. In that case, the development directory ./src is not available to the browser. When the page is loaded in browser only the files in the 'public' directory are available to the browser. So, the relative path ./src is assumed to be public/src directory and if the logo.png is not found in public/src/images/ directory, it would not render the image.

    So, the solution for this problem is either to put your image in the public directory and reference the relative path from public directory or use import or require keywords in React or any Javascript module to inform the Webpack to read this path during build phase and include the image in the final build output. The details of both these methods has been elaborated by Dan Abramov in his answer, please refer to it or use the link: https://create-react-app.dev/docs/adding-images-fonts-and-files/

    0 讨论(0)
  • 2020-11-30 23:26

    we don't need base64 , just give your image path and dimensions as shown below.

    import Logo from './Logo.png' //local path

            var doc=new jsPDF("p", "mm", "a4");
            var img = new Image();
            img.src =Logo;
            doc.addImage(img, 'png', 10, 78, 12, 15)
    
    0 讨论(0)
  • 2020-11-30 23:28

    If you have questions about creating React App I encourage you to read its User Guide.
    It answers this and many other questions you may have.

    Specifically, to include a local image you have two options:

    1. Use imports:

      // Assuming logo.png is in the same folder as JS file
      import logo from './logo.png';
      
      // ...later
      <img src={logo} alt="logo" />
      

    This approach is great because all assets are handled by the build system and will get filenames with hashes in the production build. You’ll also get an error if the file is moved or deleted.

    The downside is it can get cumbersome if you have hundreds of images because you can’t have arbitrary import paths.

    1. Use the public folder:

      // Assuming logo.png is in public/ folder of your project
      <img src={process.env.PUBLIC_URL + '/logo.png'} alt="logo" />
      

    This approach is generally not recommended, but it is great if you have hundreds of images and importing them one by one is too much hassle. The downside is that you have to think about cache busting and watch out for moved or deleted files yourself.

    Hope this helps!

    0 讨论(0)
  • 2020-11-30 23:29

    Best approach is to import image in js file and use it. Adding images in public folder have some downside:

    • Files inside public folder not get minified or post-processed,

    • You can't use hashed name (need to set in webpack config) for images , if you do then you have to change names again and again,

    • Can't find files at runtime (compilation), result in 404 error at client side.

    0 讨论(0)
  • 2020-11-30 23:34

    In order to load local images to your React.js application, you need to add require parameter in media sections like or Image tags, as below:

    image={require('./../uploads/temp.jpg')}
    
    0 讨论(0)
  • 2020-11-30 23:35

    First, you need to create a folder in src directory then put images you want.

    Create a folder structure like

    src->images->linechart.png
    

    then import these images in JSX file

    import linechart from './../../images/linechart.png'; 
    

    then you need use in images src like below.

    <img src={linechart} alt="piechart" height="400px" width="400px"></img>
    
    0 讨论(0)
提交回复
热议问题