0

How do I write a function that takes an array of components and instantiates them all in its enclosing scope?

const = [component1, component2,...]

// into

<Component1 />
<Component2 />
...
6
  • [Component1, Component2].map(Component => <Component />) Commented Mar 2, 2021 at 0:45
  • @AndrewLi wouldn't that give me another array of components? Commented Mar 2, 2021 at 1:08
  • 1
    Should just be able to wrap them in a div. <div>{components}</div>. Commented Mar 2, 2021 at 1:16
  • @BradyHaden Thanks! How could I do that with a for loop? What would I return? Commented Mar 2, 2021 at 1:29
  • 1
    @BradyHaden I want to take an array of components passed as props into another component and make them share data. I wanted to be able to wrap them in a Context Provider. Commented Mar 2, 2021 at 1:54

2 Answers 2

2

If you have your components with their props ready to use in the array you can use the array itself, cause it works the same as Fragment. If you need to pass props to your component you can map through the array and pass props.

const Todo = ({id, done, text}) =>  (
  <li key={id}>
    <label>
      <input type="checkbox" disabled readOnly checked={done} /> 
      <span className={done ? "done" : ""}>{text}</span>
    </label>
  </li>
)

const Todos = [
    // Render with jsx
    <Todo text="Learn React" done={false} id={2} />,
    // Render with createElement
    React.createElement(Todo, {text: "Learn JavaScript", done: false, id: 1}),
    // Render with function call (not possible all the time e.g. some ref cases)
    Todo({text: "Play around in JSFiddle", done: true , id: 3}),
]
const TodoApp = () =>  (
<ol>
 {[Todos]}
</ol>
)

Of course, you can alter the array before committing to the screen

const FancyMapperComponent({size}) {
  return (
    <ul>
      {Array.from({length:size}).map((_, i) => (
        <li key={`unique-${i}`}>Text content {i}<li/>
      )}
    </ul>
  )
}

demo in jsfiddle

Sign up to request clarification or add additional context in comments.

2 Comments

How could I generate them in a way that lets me change their prop dynamically from the enclosing component?
you can map through a list of props and return newly generated component with dynamic props
2
const Example = (props) => {
  const context = {};

  const newProps = {
    a: '1',
    b: '2',
  }
  
  return (
    <Context.Provider value={context}>
      {[Component1, Component2].map((Component) => (
        <Component {...newProps} />
      ))}
    </Context.Provider>
  );
};

4 Comments

Thank you! Would it work to wrap them in a Context Provider?
Oh bless you kind sir!
Is there any way to generate them in a way that lets me change their props dynamically from the enclosing component?
@S.W. sure, I've updated my example code. You can define the new props, and then use the spread ... operator to add them to your child components.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.