Answer the question
In order to leave comments, you need to log in
How to do a render in a loop in React?
Stupid question, but I can't solve the problem.
If there was an array it would be done like this
render() {
return(
<div >
{this.props.list.map(function(city) {
return <div></div>
}.bind(this))}
</div>
)
}
render() {
return(
<div >
for(let i = 0; i <2 ; i++) {
return<div>{i}</div>
}
</div>
)
}
Answer the question
In order to leave comments, you need to log in
You do not need to render inside the component with an interval, but redraw the component outside, passing different props.
The most simplified version:
class MyContainer extends Component {
constructor() {
this.state = { nums: [] }
}
componentDidMount() {
this.intervalId = setInterval(() => {
this.setState({
nums: [
...this.state.nums,
(this.state.nums[this.state.nums.length-1] || 0) + 1
],
})
}, 1000)
}
componentWillUnmount() {
clearInterval(this.intervalId)
}
render() {
retrurn (
<div>
{this.state.nums.map((n, i) => (<b key={i}>Number is: {n}</b>)}
</div>
)
}
}
render() {
return (
<div>
${Array.from({length: 5}).map((_, idx) => <div>{idx}</div>)}
</div>
);
}
// или
render() {
const list = [];
for (let idx = 0; idx < 5; idx++ ){
list.push(<div>{idx}</div>);
}
return <div>${list}</div>;
}
The best choice is 2 components.
Just make a parent, and next to it, place its child and display information in it. In the parent, call your child in the loop and there will be no problem. So it's more correct.
Most likely, these blocks will not be empty, but with some content?
In this case, pass the newly received data to the props
component, and already inside it, use the map
components to collect as much as necessary.
I practically talked about the example that you yourself gave as a worker.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question