Answer the question
In order to leave comments, you need to log in
How to update nested array in React?
Good day!
I need to change the state of an element nested in an array, namely questions_count:
this.state = {
data: [],
isLoaded: false,
content: {
exams:[],
subjects: [],
numbers: [],
themes: [
{
id: 16,
questions_count: 8,
title: "Квадратные ура…"
},
{
id: 14,
questions_count: 2,
title: "Арифметическая…"
},
{
id: 15,
questions_count: 2,
title: "Геометрическая…"
}
]
}
this.setState({
content.themes[i].questions_count: content.themes[i].questions_count++
})
this.setState({
...this.state, content: {
...this.state.content, themes[i]: {
...this.state.content.themes[i], questions_count: questions_count++
}
}
})
Answer the question
In order to leave comments, you need to log in
Unfortunately, you will have to completely copy the content object with the new data in the array.
const newContent = { ...this.state.content };
newContent.themes[0].questions_count++;
this.setState({ content: newContent });
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question