Answer the question
In order to leave comments, you need to log in
Why don't React developers write for, while loops?
For example, I have never seen anyone write such cycles in React code, which means they are either written very rarely or not written at all. Why is that? Does this go against the principles of react?
Answer the question
In order to leave comments, you need to log in
hulktoster , map/filter/reduce do just what is needed in React - they convert an array with data into an array with React components, of course, you can also through cycles, but this will be more scribbling and will be less visual
data = [........]
render() {
return (
<>
{ data.map(x => <MyComponent prop1={x.prop1} prop2={x.prop2} />) }
</>
)
}
data = [........]
render() {
const components = []
for(let i = 0; i < data.length; i++) {
const x = data[i]
components.push(<MyComponent prop1={x.prop1} prop2={x.prop2} />)
}
return (
<>
{ components }
</>
)
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question