简体   繁体   English

React Hooks:更改多个项目的 state

[英]React Hooks : Change state of multiple items

I have a list of icons.我有一个图标列表。 When I click on one of them, I want it to:当我单击其中一个时,我希望它:

1/ change state to day or night 1/ 将 state 更改为白天或黑夜

2/ change icon image to a sun or a moon. 2/ 将图标图像更改为太阳或月亮。

But with the code that I've written, if I click on one, it changes all the icons image and only one state is linked to all.但是使用我编写的代码,如果我单击一个,它会更改所有图标图像,并且只有一个 state 链接到所有图标。

How can I do so each icons has it own state and when i click on icon, only the icon clicked changes to a moon or a sun and not all of them?我该怎么做每个图标都有它自己的 state 并且当我点击图标时,只有点击的图标会变成月亮或太阳,而不是全部?

My code:我的代码:

export default function MyModal() {
  const [isVisible, setVisible] = useState(false);
  const [isDay, setDay] = useState(true);
  const { RangePicker } = DatePicker;
  const { Option } = Select;
  const style = {
    verticalAlign: "middle",
    marginRight: 10,
  };

  function handleDayNight() {
    isDay === true ? setDay(false) : setDay(true);
    console.log("isDay =", isDay);
  }
  function handleSelectChange(value) {
    console.log(`selected ${value}`);
  }

  function handleCheckChange(checkedValues) {
    console.log("checked =", checkedValues);
  }

  return (
    <div>
      <Button type="primary" onClick={() => setVisible(true)}>
        Add an exception
      </Button>
      <Modal
        title="Add an exception"
        style={{ top: 20 }}
        visible={isVisible}
        onOk={() => setVisible(false)}
        onCancel={() => setVisible(false)}
      >
        <p>Exceptions name</p>
        <Input placeholder="80% Wednesday" />
        <p style={{ marginTop: 10 }}>Select date</p>
        <RangePicker onChange={([date]) => console.log(date)} />
        <p style={{ marginTop: 10 }}>Frequency</p>
        <Select
          defaultValue="Weekly"
          style={{ width: 120 }}
          onChange={handleSelectChange}
        >
          <Option value="daily">Daily</Option>
          <Option value="weekly">Weekly</Option>
          <Option value="monthly">Monthly</Option>
        </Select>
        <Divider />
        <Checkbox.Group style={{ width: "100%" }} onChange={handleCheckChange}>
          <div>
            {isDay === true ? (
              <Sun style={style} onClick={handleDayNight} />
            ) : (
              <Moon style={style} onClick={handleDayNight} />
            )}
            <Checkbox value="Monday">Monday</Checkbox>
            <br></br>
            {isDay === true ? (
              <Sun style={style} onClick={handleDayNight} />
            ) : (
              <Moon style={style} onClick={handleDayNight} />
            )}
            <Checkbox value="Tuesday">Tuesday</Checkbox>
            <br></br>
            {isDay === true ? (
              <Sun style={style} onClick={handleDayNight} />
            ) : (
              <Moon style={style} onClick={handleDayNight} />
            )}
            <Checkbox value="Wednesday">Wednesday</Checkbox>
            <br></br>
            {isDay === true ? (
              <Sun style={style} onClick={handleDayNight} />
            ) : (
              <Moon style={style} onClick={handleDayNight} />
            )}
            <Checkbox value="Thursday">Thursday</Checkbox>
            <br></br>
            <Checkbox value="Friday">Friday</Checkbox>
            <br></br>
            <Checkbox value="Saturday">Saturday</Checkbox>
            <br></br>
            <Checkbox value="Sunday">Dimanche</Checkbox>
          </div>
        </Checkbox.Group>
      </Modal>
    </div>
  );
}

Use a separate component for each of those, so you can have individual separate states inside those components.为每个组件使用单独的组件,因此您可以在这些组件中拥有单独的单独状态。 Eg, replace all of例如,替换所有

{isDay === true ? (
    <Sun style={style} onClick={handleDayNight} />
) : (
    <Moon style={style} onClick={handleDayNight} />
)}

with

<SunMoon style={style} />
const SunMoon = ({ style }) => {
  const [isDay, setDay] = useState(true);
  const handleDayNight = () => setDay(!isDay);
  return isDay
    ? <Sun style={style} onClick={handleDayNight} />
    : <Moon style={style} onClick={handleDayNight} />;
};

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

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