Answer the question
In order to leave comments, you need to log in
Why is the component not being rendered after changing the state?
There was a problem that after changing the state (debugging via console.log), the component does not rerender.
The component itself and its children depend on the state:
<div className="main-board">
{this.state.cols.map((col, index) => {
return (
<Column
key={index}
id={index}
name={col.name}
colId={index}
col={this.state.cols[index]}
cards={this.state.cols[index].cards}
addCard={this.insertNewCard}
deleteCard={this.deleteHandler}
/>
)
})}
insertNewCard = (id) => {
this.setState((prevState) => {
prevState.cols[id].cards = [
...prevState.cols[id].cards,
{
id: this.getNewId(),
date: '11.11.1234',
grade: '10',
name: 'Созданный',
},
]
return {
cols: prevState.cols,
}
})
}
insertNewCol() {
console.log('insertNewCol')
this.setState((prevState) => {
return {
cols: [
...this.state.cols,
{
name: 'Созданная колонка',
cards: [
{
id: this.getNewId(),
date: '00.00.9999',
grade: '1',
name: 'Новая карточка',
},
],
},
],
}
})
}
Answer the question
In order to leave comments, you need to log in
cols: prevState.cols,
this.setState((prevState) => {
return {
cols: prevState.map((col, index) => {
if (index !== id) {
return col;
}
return {
...col,
cards: [
...col.cards,
{/*new card fields*/}
]
}
}
}
})
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question