Ad

Add Props To React Render Call With Looping Construct

- 1 answer

Say I have an array ("props") that I want to render a React component with. Something like:

const props = [];

ReactDOM.render(<Comp prop0={props[0]} prop1={props[1]/>, document.getElementById('foo'));

Is there acceptable way to add props to a React component using some loop construct instead of "hardcoding" it?

Ad

Answer

Usually, you would use the spread operator there:

var props = {};
props.foo = 'something';
props.bar = 'something else';

ReactDOM.render(<Component {...props} />, someDomElement);
Ad
source: stackoverflow.com
Ad