score:8
What worked for me:
render() {
const components = [ComponentA, ComponentB, ComponentC] // references to components
return (
<div>
{components.map((comp, i) => React.createElement(comp, { key: i })}
</div>
);
}
Had to use a reference to the component and had to use React.createElement
due to problems with nested JSX (might be Typescript related).
score:0
For some reason using the map function as specified in the React documentation didn't work for me for any component/element that's not an li, I had to explicitly specify a return statement.
options = optionsArray.map(o => {
// replace option with your component name
return <option key={o.name} value={o.name}>{o.name}</option>
})
then I rendered the generated elements. You can do it inline too if you want.
<div>
{options}
</div>
score:1
Try this. It should work.
render()
{
const input=["ComponentA", "ComponentB", "ComponentC"]
return(
<div>
{
input.map((comp,i)=>){
return <{comp} key={i} />
}
}
</div>
)
}
Note: input= ["ComponentA", "ComponentB", "ComponentC"];Custom component name should be in Capital
score:1
render() {
return (
<div>
{componentsToRender.map((Component, key) => (<Component key={key}/>))}
<div>
);
}
Source: stackoverflow.com
Related Query
- React - Render dynamic list of components
- Render dynamic list of react components from array of strings
- Render a dynamic list in React
- React - create dynamic list of child components with onClick
- Render dynamic components by names in React
- My React list item components don't render one at a time
- Is it possible to hold a list of react components in state and render them?
- React - Render dynamic components based on API response
- react render list of components using spread attributes
- How to render react components by using map and join?
- How to create dynamic href in react render function?
- Rendering React components with promises inside the render method
- Is there a way to render multiple React components in the React.render() function?
- Using for loops and switch cases in React to dynamically render different components
- React render array of components
- Dynamic loading of react components
- Render multiple components in React Router
- How to sort list of React components based on different properties?
- React - Render a certain number of components dynamically
- HOC's and Render Props With Functional Components in React 16
- How to provide dynamic className to element in React Class render method
- React Router v4 components with similar paths (fixed and dynamic path param) are "overlapping"
- Why do React components that throw errors render twice?
- How I can render react components without jsx format?
- How to add components to list dynamically in React with no redundant render?
- How to render React components only once on startup?
- React - Dynamic creation of List item inside component
- React lazy components not loaded on dynamic routes
- How to render JSON response as dropdown list in React
- React router render prop route always re-renders components
More Query from same tag
- Prevent React from importing CSS files that are not being used yet
- How to add link to a string in react
- Material UI Select in Typescript
- Lazy loading in react - the component is not loading
- How to append to dom in React?
- How to toggle between display: none and display:block on click
- how to redirect the users once user has logged in and to get photo ,name and other detaills of the user
- Looking to pass all props using react hooks
- How to extend Link and NavLink with same styles using Styled Components
- How to use Apollo/Graphql hooks within external React.js component library
- How to detect if mobile keyboard is shown in Web?
- Fetch API - How to transform JSON into HTML
- How can I filter 2 object arrays in React?
- How to mount component only when i change class props
- Why do I need to pass an anonymous function into the onClick event?
- Internal styling in React
- Augment react-router module with react-router-relay typings
- How can I get cookies from Express API to React?
- How to reset the state to it's initial state in redux store?
- Prevent auto focus of a material-ui popover element
- Component does not see global styles
- How to add react-router-link to mapbox marker created with forEach method
- React js css stylesheet applied to more than one component
- Prevent default onpaste
- Why is Error boundary in React JS not working
- Redux Form Checkbox Validation
- How to manipulate attributes from ref selector
- How use on change event in reactjs (hooks)
- :hover and :focus inline style in React doesn't work
- Redirect After Authentication Not Working. No Path in History