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

前端 未结 5 1682
挽巷
挽巷 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:38

    You can simply make a function which takes a parameter "name" (your svg icon name) and return your svg code.

    import React from 'react' export function getIcon(name){ switch(name) { case 'back': return ( // your svg code here ) } }

    And then you can import it anywhere and simply call it with icon name you want.

    import { getIcon } from './utils'
    render() {
      return (
        
    { getIcon('back') }
    ) }

提交回复
热议问题