下面的代码是我试图在状态下更新main_id
和sub_ids
的代码。
我被困在这里了。。。
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: []})
))})
}
我在同一个组件上使用这个函数,这意味着它添加了具有不同对象的数组的特定索引。
<componentOne onChange ={handleState(k)} />
<componentTwo onChange={handlestate(k)} />
state = {
ids:[
{
main_id: v.id,
sub_ids:[]
},
{
main_id: v.id,
sub_ids:[]
}
]
}
请参阅以下代码:
const [state, setState] = useState({
ids: [
{
main_id: null,
sub_ids: []
}
]
});
// this is what I've tried..
const handleState = index => (v) => {
setState({ ...state, ids: state.ids.map((x, i) => (
i === index ? { main_id: v.id, sub_ids: [] } : x
))})
}