Reactjs 带有Typescript和React钩子的多个选中项

Reactjs 带有Typescript和React钩子的多个选中项,reactjs,typescript,input,react-hooks,checked,Reactjs,Typescript,Input,React Hooks,Checked,我已经苦思冥想了一段时间,但还是想不出来 我复制了互联网上的一些解决方案,但都不起作用 我有一个包含此代码的父组件 const [ solutionsSelectedForPublishing, setSolutionsSelectedForPublishing, ] = useState<Array<string>>([]); ... const handleRowSelectionClick = (e: any) => { const newSt

我已经苦思冥想了一段时间,但还是想不出来

我复制了互联网上的一些解决方案,但都不起作用

我有一个包含此代码的父组件

const [
  solutionsSelectedForPublishing,
  setSolutionsSelectedForPublishing,
] = useState<Array<string>>([]);

...

const handleRowSelectionClick = (e: any) => {
  const newState = [...solutionsSelectedForPublishing];
  if (e.target.checked) {
    newState.push(e.target.value);
  } else {
    newState.filter((prev) => prev !== e.target.value);
  }
  setSolutionsSelectedForPublishing(newState);
};

const[
选择用于发布的解决方案,
设置选择用于发布的解决方案,
]=使用状态([]);
...
const handleRowSelectionClick=(e:any)=>{
const newState=[…为发布选择的解决方案];
如果(例如,选中目标){
newState.push(即target.value);
}否则{
newState.filter((prev)=>prev!==e.target.value);
}
设置为发布选择的解决方案(newState);
};
在子组件上


我遇到的问题是,添加到SolutionSelectedForPublishing数组的项不会持久化。只出现一次,当我单击另一个检查输入时,它将替换该项,而不是将其添加到数组中。我不明白为什么,我尝试了这么多东西…

我找到了解决办法。这很有魅力

setSolutionsSelectedForPublishing((solutionsSelectedForPublishing) => [
  ...solutionsSelectedForPublishing,
  ...newState,
]);