简体   繁体   English

React - 如何在不使用地图功能的情况下基于一个选择所有复选框

[英]React - How to select all checkboxes based on one without using the map function

I created a simple form in React that displays the submitted data in the console.我在 React 中创建了一个简单的表单,在控制台中显示提交的数据。 I created three checkboxes, the first one to mark the two below, and the other two regarding data processing.我创建了三个复选框,第一个用于标记下面的两个,另外两个用于数据处理。 Is there a way to change the state of two checkboxes by clicking only the "Accept All" checkbox?有没有办法通过仅单击“全部接受”复选框来更改两个复选框的状态?

I found solutions that worked on checkboxes mapped from array, while my checkboxes are simply written in the code.我找到了适用于从数组映射的复选框的解决方案,而我的复选框只是写在代码中。 I can't understand how I can target each of the two checkboxes that I need to make change in based on "Accept all" checkbox我不明白如何根据“全部接受”复选框来定位需要更改的两个复选框中的每一个

Below is a styled component code that includes Processing div, label, checkbox and text for checkboxes下面是一个样式化的组件代码,其中包括处理 div、标签、复选框和复选框的文本

import styled from 'styled-components';

export const Processing = styled.div`
  display: flex;
  flex-direction: column;
  gap: 24px;

  @media (min-width: ${({ theme }) => theme.tabletPortrait}) {
    flex: 1;
  }
`;

export const Label = styled.label`
  display: flex;
  align-items: flex-start;
  gap: 10px;
`;

export const Checkbox = styled.input`
  width: 24px;
  height: 24px;
`;

export const CheckboxText = styled.span`
  font-size: 2rem;
  font-weight: 300;
  font-family: 'Mulish', sans-serif;
  color: ${({ theme }) => theme.colors.white};
`;

Here is the Contact.jsx component这是 Contact.jsx 组件

import {
  Checkbox,
  CheckboxText,
  Label,
  Processing,
} from './styles/Contact.styled';

const Contact = () => {
  return (
    <Processing>
      <Label>
        <Checkbox type='checkbox' />
        <CheckboxText>Accept all</CheckboxText>
      </Label>
      <Label>
        <Checkbox type='checkbox' defaultChecked={false} required />
        <CheckboxText>I agree to the processing my data</CheckboxText>
      </Label>
      <Label>
        <Checkbox type='checkbox' defaultChecked={false} required />
        <CheckboxText>RODO processing my data by company</CheckboxText>
      </Label>
    </Processing>
  );
};

export default Contact;

To control the state of another component you will have to use a controlled component.要控制另一个组件的状态,您必须使用受控组件。 Usually it looks like this:通常它看起来像这样:

const Contact = () => {
  const [isChecked, setIsChecked] = useState(false);

  // Or whatever your prop names are
  // The point is that Contact decides what the value of the checkbox should be
  // and Checkbox reports back whenever it changes
  return <Checkbox onChange={setIsChecked} value={isChecked} />
};

Now the parent component controls the value of the checkbox, it doesn't manage its own state.现在父组件控制复选框的值,它不管理自己的状态。 Which means you can write functions that change that state in the parent component.这意味着您可以编写函数来更改父组件中的状态。

const Contact = () => {
  const [checks, setChecks] = useState([false, false, false]);

  useEffect(() => {
    if (checks[0]) setChecks([true, true, true]);
    else setChecks([false, false, false]);
  }, [checks[0]]);

  const handleOnChange = n => () => {
    // Or however you want to update a single value in a list
    setChecks(c => [...c.slice(0, n), !checks[n], ...c.slice(n + 1)]);
  };

  return (
  <Processing>
    <Label>
      <Checkbox onChange={handleOnChange(0)} type='checkbox' />
      <CheckboxText>Accept all</CheckboxText>
    </Label>
    <Label>
      <Checkbox onChange={handleOnChange(1)} type='checkbox' defaultChecked={false} required />
      <CheckboxText>I agree to the processing my data</CheckboxText>
    </Label>
    <Label>
      <Checkbox onChange={handleOnChange(2)} type='checkbox' defaultChecked={false} required />
      <CheckboxText>RODO processing my data by company</CheckboxText>
    </Label>
  </Processing>
  );
};

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

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