简体   繁体   English

如何为一个元素而不是所有元素更改复选框 state

[英]How to change checkbox state for one element instead of all

I am trying to change the state of a checkbox when I have two, but all checkboxes are being checked at the same time, I tried different solutions for 5 days and still nothing... I'm quite new to react so I'm lost.当我有两个复选框时,我正在尝试更改复选框的 state,但是同时检查了所有复选框,我尝试了 5 天不同的解决方案,但仍然没有...丢失的。

import React, { ChangeEvent, useCallback, useState } from 'react';
import ReactDOM from 'react-dom';
import { Checkbox, Pane } from 'evergreen-ui';
 
function ControlledCheckboxExample() {
  const [checkedItems, setCheckedItems] = React.useState(false)

  const handleButtonClick = (e) => {
    console.log(!checkedItems, e);
    setCheckedItems(!checkedItems);
  };

  return (
    <Pane>
      <Checkbox
        label="Controlled usage"
        name="afaf"
        key={1}
        checked={checkedItems}
        onChange={handleButtonClick.bind(name, 1)}
      />
      <Checkbox
        label="Controlled usage"
        name="afatrf"
        key={2}
        checked={checkedItems}
        onChange={handleButtonClick.bind(name, 2)}
      />
    </Pane>
  )
}

ReactDOM.render(
  <ControlledCheckboxExample />,
  document.getElementById("root")
)

This is my code, is there any solution you can propose?这是我的代码,您可以提出任何解决方案吗?

Issue问题

The code is using and updating a single state for all checkbox inputs.该代码正在为所有复选框输入使用和更新单个 state。

Solution解决方案

Convert the checkedItems to an object of booleans and use the onChange event object and the input name to toggle a specific input.checkedItems转换为布尔值的 object 并使用onChange事件 object 和输入名称来切换特定输入。

Example:例子:

function ControlledCheckboxExample() {
  const [checkedItems, setCheckedItems] = React.useState({});

  const handleButtonClick = (e) => {
    const { name } = e.target;
    setCheckedItems(checkedItems => ({
      ...checkedItems,
      [name]: !checkedItems[name]
    }));
  };

  return (
    <Pane>
      <Checkbox
        label="Controlled usage"
        name="afaf"
        key={1}
        checked={checkedItems["afaf"]}
        onChange={handleButtonClick}
      />
      <Checkbox
        label="Controlled usage"
        name="afatrf"
        key={2}
        checked={checkedItems["afatrf"]}
        onChange={handleButtonClick}
      />
    </Pane>
  );
}

编辑 how-to-change-checkbox-state-for-one-element-instead-of-all

You are using same state variable for both checkboxes and of course if you click on one the second will be set too.您为两个复选框使用相同的 state 变量,当然,如果您单击一个,第二个也将被设置。 Create another state variable for another checkbox or use an array like so为另一个复选框创建另一个 state 变量或使用这样的数组

const [state, setState] = React.useState(new Array({length of how much boxes you have}).fill(false);

and then update state然后更新 state

const handleOnChange = (position) => {
const updatedCheckedState = checkedState.map((item, index) =>
  index === position ? !item : item
);

setCheckedState(updatedCheckedState);

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM