Answer the question
In order to leave comments, you need to log in
Why is React re-rendering infinite times?
Good afternoon.
Mastering ToDo App. By clicking "Add note" the corresponding action is dispatched, everything is fine here.
I want to store the list of ready-made notes in an array, and draw them through .map(), and so that after each addition of a note and redrawing, the notes array is not reset, I write it to state.
But, react throws an error:
Too many re-renders. React limits the number of renders to prevent an infinite loop.
import React, {useState} from 'react'
import './Notes.css'
import { connect } from 'react-redux';
const Notes = (props)=>{
let [notes, setNotes] = useState();
let key = 0;
setNotes(123123)
console.log(props.notes)
/*for(let i = 0; i<16; i++){
notes.push(<div key={++key} className="item">Lorem ipsum dolor sit amet, consectetur adipisicing elit.</div>)
}*/
return (
<div className="masonry">
{ console.log(props.notes)} {
/*props.notes.map(
(e)=>{
return <div key={++key} className="item">{e[0]} - {e[1]}</div>
}
)*/
}
</div>
)
}
const mapStateToProps = (state)=>{
return {
notes: state
}
}
export default connect(mapStateToProps)(Notes)
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question