javascript - How to import an entire folder of SVG images (or how to load them dynamically) into a React Web App? -
i have component takes in :itemname , spits out html bundle containing image. image different each bundle.
here's have:
import react, { component } 'react'; import { navlink } 'react-router-dom'; import svginline "react-svg-inline"; export default (props) => ( <navlink classname="hex" activeclassname="active" to={'/hex/' + props.itemname}> {react.createelement(svginline, {svg: props.itemname})} </navlink> )
how make component work?
i know if imported images explicitly, call images so...
import svginline "react-svg-inline"; import sasssvg "./images/sass.svg"; <navlink classname="hex" activeclassname="active" to="/hex/sass"><svginline svg={ sasssvg } /></navlink>
this work, since need include ~60 svgs, adds lot of excess code.
also, found in question code...
import * iconid './icons';
but doesn't seem work (it part of question, not answer), , answer bit nonspecific answer question i'm asking.
i found this question again there's answer (although unapproved) possess more questions answers. so, after installing react-svg, set test see if answer works so...
import react, { component } 'react'; import reactdom 'react-dom' import { navlink } 'react-router-dom'; import reactsvg 'react-svg' export default (props) => ( <navlink classname="hex" activeclassname="active" to={'/hex/' + props.itemname}> <reactsvg path={"images/" + props.itemname + ".svg"} callback={svg => console.log(svg)} classname="example" /> </navlink> )
but, op of question asking, page can't find svg after copying entire image folder build folder. i've tried "./images/"
i feel i'm missing 1 last key piece of information , after searching past day, hoping identify piece i'm missing.
if using react, suspect using webpack. can use require.context
instead of es6 import
, webpack resolve when building.
this first line import. grab files ending in .svg
images
folder. change 2nd parameter false
if don't want recursive folder scanning.
const svgs = require.context('./images', true, /\.svg$/)
this gives function
keys
property , id
property. can pass filepath function image. keys
property gives list of valid filepaths. id
property webpack module id. might easier see in example: survivejs webpack dynamic loading
this api limited , perhaps unintuitive, want convert data structure. here 3 examples might useful.
create array of files. can grab index number keys
array if need access image pathname.
const svgs = require.context('./images', true, /\.svg$/) const keys = svgs.keys() const svgsarray = keys.map(key => svgs(key))
create array of objects, each object being {path, file}
1 image
const svgs = require.context('./images', true, /\.svg$/) const svgsobjarray = svgs.keys() .map(key => { path: key, file: svg(key), })
create object pathname key matching file.
const svgs = require.context('./images', true, /\.svg$/) const svgsobj = svg.keys .map(key => [key, svgs(key) ) .reduce(images, img => { images[img[0]] = img[1] return images }, {})
Comments
Post a Comment