The code below is what I'm trying to do update main_id and sub_ids in the state.
I got stucked from here...
const [state, setState] = useState({
ids: [
{
main_id: null,
sub_ids: []
}
]
});
//this is what I've tried..
const handleState = index => (v) => {
setState({...setState, ids: setState.ids.map((x,i)=>(
i === index ? {x.main_id: v.id, sub_ids: []})
))})
}
I'm using this function on same component which means it adds in specific index of array with different object.
<componentOne onChange ={handleState(k)}/>
<componentTwo onChange={handlestate(k)} />
The state that I desired to get after this,
state ={
ids:[
{
main_id: v.id,
sub_ids:[]
},
{
main_id: v.id,
sub_ids:[]
}
]
}