How to import an entire folder of SVG images (or how to load them dynamically) into a React Web App?

前端 未结 5 1694
挽巷
挽巷 2021-02-05 05:02

I have a component that takes in an :itemName and spits out an html bundle containing an image. The image is different for each bundle.

Here\'s what I have:



        
5条回答
  •  你的背包
    2021-02-05 05:34

    Instead of multiple SVG files you can use the single SVG sprite.

    SVG sprite can be generated from a directory of SVG files using svg-sprite-generator:

    svg-sprite-generate -d images -o images/sprite.svg
    

    Then use it like this:

    import React from 'react';
    import { NavLink } from 'react-router-dom';
    import sprite from './images/sprite.svg';
    
    export default (props) => (
      
        
          
        
      
    )
    

提交回复
热议问题