简体   繁体   English

为什么需要点击两次才能更新 state? (React.js, React Hooks)

[英]Why need to click twice to update the state? (React.js, React Hooks)

I am trying to create Update Form Modal for edit the original data.我正在尝试创建更新表单模式以编辑原始数据。 My code actually works, but there is a bug which requires the button to be double-clicked to display data from API (getTeamsById()).我的代码实际上可以工作,但是有一个错误需要双击按钮才能显示来自 API (getTeamsById()) 的数据。

The Button按钮

<button
  type="button"
  className="btn"
  onClick={() => handleUpdateTeams(team.id)}
>
  <img
    src="/img/icon/ic_edit.png"
    alt="edit"
    className="container-img"
  />
</button>

handleUpdateTeams()处理更新团队()

  const handleUpdateTeams = (teamId) => {
    dispatch(getTeamsById(teamId))
      .then(() => {
        setId(teamId);
        let data = myTeam.userData;
        setPhone(data.user.phone);
        setEmail(data.user.email);
        setFullName(data.user.fullName);
        setDateOfBirth(moment(data.user.birthday).format("YYYY-MM-DD"));
        setGender(data.user.gender);
        setDescription(data.description);
        setSpecialist(data.specialist);
        setKTPNumber(data.KTPNumber);
        setSIPNumber(data.SIPNumber);
        setSTRNumber(data.STRNumber);
        setWorkingExperience(data.workExperience);
        setServiceFee(data.serviceFee);
        setAvatar(data.user.avatar);
        setKtp(data.KTPFile);
        setSip(data.SIPFile);
        setStr(data.STRFile);
      })
      .then(setUpdateTeamsModal(true));
  };

The Modal模态

  const renderUpdateTeams = () => {
    return (
      <Modal
        modalTitle="Update Healthcare Professional"
        show={updateTeamsModal}
        size="lg"
        handleClose={() => handleCloseModal()}
        buttons={[
          {
            label: "Update",
            color: "warning",
            onClick: actionUpdateTeams,
          },
        ]}
      >
        <Row>
          <Col md>
            <Input
              name="avatar"
              label="Profile Picture"
              type="file"
              accept="image/*"
              onChange={handleProfilePicture}
            />
          </Col>
          <Col md>
            <Input
              label="Full Name"
              placeholder="Input Full Name"
              type="text"
              value={fullName}
              onChange={(e) => setFullName(e.target.value)}
            />
          </Col>
        </Row>

    .....

    <Row>
          <Col md>
            <Input
              label="Service Fee"
              placeholder="Input Service Fee"
              type="number"
              min="0"
              value={serviceFee}
              onChange={(e) => setServiceFee(e.target.value)}
            />
          </Col>
        </Row>
      </Modal>
    );
  };

Do you have any solution for this problem/bug?, all suggestions will be very helpful.您对此问题/错误有任何解决方案吗?所有建议都会非常有帮助。 By the way I am using react-redux for state management.顺便说一句,我正在使用 react-redux 进行 state 管理。

Your handleUpdateTeams function calls dispatch which causes the myTeam variable from useSelector to update, but your .then() callback is a closure created with the old value of myTeam .您的handleUpdateTeams function 调用dispatch会导致来自useSelectormyTeam变量更新,但您的 .then .then()回调是使用myTeam的旧值创建的闭包。 So you are setting your component states based on the old Redux state rather than the new state.因此,您正在根据旧的 Redux state 而不是新的 state 设置组件状态。

You need to either:您需要:

  • Get the new data value from the return value of the dispatch .dispatch的返回值中获取新的data值。
  • Respond to changes in myTeam.userData after they occur by using a useEffect hook with myTeam.userData as a dependency.通过使用带有myTeam.userData作为依赖项的useEffect挂钩,对myTeam.userData的更改进行响应。

As a sidenote, having 26 individual states is crazy.作为旁注,拥有 26 个独立的州是疯狂的。 I think you could use some objects here.我想你可以在这里使用一些对象。

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

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