score:0
If you want to avoid doing a switch, you could build an object with your imports:
const icons = {
svg1,
svg2,
svg3,
};
const Child = (props) => {
const Component = icons[props.icon];
return <Component />;
};
score:0
You could try using the image tag, like this;
import svg1 from './svg1.js'
import svg2 from './svg2.js'
import svg3 from './svg3.js'
<Parent>
<Child icon={svg1} />
<Child icon={svg2} />
<Child icon={svg3} />
</Parent>
Child
return (
<div>
<img src={output} />
</div>
)
score:0
Based on this one - https://sung.codes/blog/2017/12/03/loading-react-components-dynamically-demand/
I finally get the solution:
const { icon } = props
const [components, setComponents] = useState([])
const addComponent = async (icon) => {
import(`./${icon}.js`)
.then((component) => {
setComponents(components.concat(component.default))
})
.catch((error) => {
console.error(`"${icon}" not yet supported`)
})
}
const getElement = async (icon) => {
await addComponent(icon)
}
useEffect(() => {
console.log("icon", icon)
getElement(icon)
}, [])
const componentsElements = components.map((Component) => <Component />)
...
return <div>{componentsElements}</div>
Source: stackoverflow.com
Related Query
- React dynamic import based on the prop from parent
- Using React Hooks, when I pass down a prop from parent to a child component, the prop in the child component is undefined
- Passing a prop from a Parent component becomes null in the Child component React
- React : set style of a children based on the prop of a parent
- What is the difference between import * as react from 'react' vs import react from 'react'
- Accessing React component children props from the parent
- CDN links for React packages and how to import it when using react using the scripts from CDN
- React import from parent directory
- React what's the right way to get a parent props from its children
- How do I manage state on a React component that can have state changed from the parent or from events upon it?
- Getting the ref from a dynamic component when using Redux, React and react-router-dom 4.x
- react js how to import only the required function from a file and not the all functions
- React Native Vector Icons to fit the parent container without specifying a size prop (Expo)
- How to update select items based on the selection from the first select items using react
- Can anyone advise on the best way to integrate React Drag/Drop Lists with dynamic values from Redux?
- Can i import the SVG file as a react component with dynamic import() Expressions?
- Directly modify parent props from the child in react
- How is an argument for a callback in React being passed from the child component to the parent component?
- Pass prop from child to parent React
- React & Typescript how get onClick prop from child to parent
- Wait for change of prop from parent component after changing it from a child in React
- How can I make a request to an API based on the response from another request? React Hook "useSwr" cannot be called inside a callback
- Refresh a React Child Functional Component based on Async input from Parent
- How to call parent function from child prop React 15.5.0
- react prop not rendering in parent component and even in the same one
- Prevent component as array element from re-render when the parent array changes in React
- How to pass input values from a child Form component to the state of its parent component for submission with react hooks?
- How to set prop in react bootstrap(Dropdown) from parent
- React router dom passing data from parent component to child router component does not pass the props.match
- React collect child component data on some event from the parent component
More Query from same tag
- .tsx webpack compile fails: Unexpected token <
- Checkbox check and uncheck to remove values in react js
- react routing. cant get this to work
- How to setup React Router <Link> inside a Datatables.net Cell?
- (React) How to put focus on first element when component updates?
- React js reduce error : Type error: produts.reduce is not a function
- Not able to click on button in ReactJS Hooks?
- React giving me Error: Too many re-renders. React limits the number of renders to prevent an infinite loop
- How to dynamically update values in Shopping Cart when one item value is changing in React JS using state in map function?
- Ajax call in react js
- Unable to access images from public folder with webpack configuration
- Why do I have to put bundle.js script in html when running webpack-dev-server
- Testing className with materialUI
- How to dispatch action in a separate, parent component
- 'onEnter' hook for Universal Router
- Redirect with state not working in React Router 4.x
- Can't get antd to work with create-react-app
- Reactjs show selected option by object
- Using the HTML5 audio tag in Reactjs
- why is my delete button in React JS not working?
- Rendering React Components By Date Order
- How to Export Multiple List in ReactJS?
- Wait for CSS to load before JS in React [FOUC]
- Exact same code from dangerouslySetInnerHTML not working only inside Gatsby / ReactJS page
- react redux accessing external api through thunk middleware
- how to properly make div tab in javascript to work with reactjs
- How do I set a scrollbar height within a reactjs return statement?
- (React/Redux) Dispatching 2 actions after a resolved promise
- Is using callback for component state update from redux saga the right way
- Package react does not satisfy its siblings