How do I update state of a nested array of object to add any empty object using typescript? I want to add to the bottomlevels in level object
how I want final state to look.
const data = {
level: {
subLevels: [
{
bottomLevels: [
{
...
},
],
},
{
bottomLevels: [
{
//I want to update the data here
},
],
},
],
},
};
types
type LevelDataType = {
id: number;
name: string;
subLevels?: SubLevelDataType[];
};
type SubLevelDataType = {
id: number;
name: string;
bottomLevels?: BottomLevelsDataType[];
subLevelId: number;
};
type BottomLevelsDataType = {
id: number;
name: string;
SubLevelId: number;
};
state
this.state = {
newData: {
level: {} as LevelDataType,
subLevels: [],
BottomLevels: [],
}
}
set state when I try to go down to a nested array object in bottomLevels to add an empty obj it doesnt let me
this.setState({
newData: {
...this.state.newData,
level: [
...this.state.newData.level,
subLevels:[
...this.state.newData.level.subLevels,
bottomLevels:[
...this.state.newData.level.subLevels.bottomLevels,
{} as bottomLevelsType
]
]
],
},
});