【问题标题】:React UseState Boolean Array Modify One ElementReact UseState 布尔数组修改一个元素
【发布时间】:2020-12-10 18:11:40
【问题描述】:

我在 useState 中有一个布尔数组

const [checkBoxState, setcheckBoxState] = useState([true,true,true,true,true,......]

我需要一个只切换特定索引值的函数像这样的函数

const HanldeCheck = (index) => {
    setcheckBoxState[index] = !checkBoxState[index];
  };

请帮忙

【问题讨论】:

    标签: reactjs checkbox boolean use-state


    【解决方案1】:

    也许你可以使用这样的东西?

    const HanldeCheck = (index) => {
        setcheckBoxState(prevState => prevState.map((item, idx) => idx === index ? !item : item))
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-07-17
      • 1970-01-01
      • 2015-01-07
      • 1970-01-01
      • 2021-09-09
      • 1970-01-01
      • 2017-11-09
      相关资源
      最近更新 更多