what i need : I am trying to display svg from external folder and that folder contains some 50 files and
public folder
|-images
-| 50 svgs
in app.js
i am trying to display the image
import React from 'react';
import './App.css';
import svgs from "../public/svgfolder/0.svg"
class App extends React.Component{
render(){
return(
<div>
<img src={svgs} alt="test"></img>
</div>
)
}
}
export default App;
i am getting below error
Module not found: You attempted to import ../public/svgfolder/0.svg which falls outside of the project src/ directory. Relative imports outside of src/ are not supported.
here i need 3 things
- How can we display 50 svgs dynamically in react ?
- some people are suggesting to make change in the web pack so is it right approach i mean will it work in production also ?
- do we have to use public folder or any other folder ?
- React support svg's ?
Note : if i call svg through url <img src={"https://s.cdpn.io/3/kiwi.svg"}/>
,
it is working and if the same using local file it is not
and if u put the svg single file src folder then a single file can be able to display
Instead of loading SVGs how you are currently doing it, I would recommend inline SVG as React Components. This way you can control the styling with props and state too as well as many other useful capabilities.
Example:
You can now import that component anywhere.
If you are using Webpack, it is better to use
svg-url-loader
for webpack & package them with your deployments.Add below in your webpack.config
app.css
app.js
React does not give access to content outside of src directory to be used in React code.
Possible solutions may be:
Move your svg inside src directory (Recommended).
Use Public folder and access it like this. (Using Public Folder)
.
I too have the same scenario where i tried this approach and it worked u can try