I am trying to setState
in an object of 2 arrays. Each array has it's own object. I am trying to create new groups and rows onClick of a button. So I have an object named groups
which has an array named firstGroup
and then another array named secondGroup
. I have a click listener handleSecondGroup
which should add a new Row to the secondGroup
. But doing a setState on the secondGroup
doesn't seem to be working. Not sure how i can achieve that.
This is what my code looks like:-
this is the App.vue file
import React, {Component} from 'react';
import './App.css';
class App extends Component {
state = {
groups {
firstGroup: [
{ id: 0, title: 'New Group', rows: [{ id: 0,value: 'row1', options:
[1,2,3,4,5]}] }
],
secondGroup: [
{id:0 , title: 'New Group', options: [1,2,3,4,5] }
]
}
}
handleSecondGroup = () => {
const groups = [...this.state.groups.secondGroup, { id:this.state.groups.length, title: 'New Second Group', options: [1,2,3,4,5}]
this.setState({
groups
})
console.log(groups)
}
export default App;
I am not sure if this is the right way to setState in nested objects? Any help will be appreciated.