简体   繁体   English

反应 - 使用 state 更新值,或不

[英]React - using state to update values, or not

I have a form with two columns.我有一个包含两列的表格。 The left column is a proposed value for the right column.左列是右列的建议值。 I currently present both values from separate API calls.我目前从单独的 API 调用中显示这两个值。 This is working.这是有效的。

This is the current code:这是当前代码:

const dbContact = useDbContact(contact.Group); This sets the value of the current values, contact.Group from the proposed values being passed to the API call.这将设置当前值的值, contact.Group从被传递到 API 调用的建议值。

I then later use this to call the form input value:然后我稍后使用它来调用表单输入值:

<Form.Row> 
    <Form.Group as={Col} controlId="currentSecName" className="mb-2">
        <Form.Control type="input" name="1secname" value={contact.SecretaryName} readOnly />
    </Form.Group>
    <Form.Group as={Col} controlId="dbSecName" className="mb-2">
        <Form.Control type="input" name="2secname" value={dbContact.SecretaryName} readOnly />
    </Form.Group>

    <Button className="PrimaryButton" size="sm">
        Accept proposed
    </Button>
    <div class="divider" />
</Form.Row>

Now I have all this working, my next step was to make the 'accept' button update the current value with the proposed.现在我已经完成了所有这些工作,下一步是让“接受”按钮使用建议更新当前值。 My code being something like this:我的代码是这样的:

const dbContact = useDbContact(contact.Group);
const [dbSecName, setDbSecName] = useState(dbContact.SecretaryName)

and

<Form.Control type="input" name="2secname" value={dbSecName} readOnly />

which would allow me to change the state value 'onClick'这将允许我更改 state 值“onClick”

When I do this though, I get a 'dbContact.SecretaryName is undefined' .但是,当我这样做时,我得到一个'dbContact.SecretaryName is undefined' Now, I'm assuming that considering I asked for the value in the form directly, during testing, I was lucky that the value was populated before being asked for so there's potentially something wrong with the API call.现在,我假设考虑到我直接在表单中询问了值,在测试期间,我很幸运在被要求之前填充了该值,因此 API 调用可能存在问题。

Here is the API call:这是 API 调用:

  async function getData(group) {
    try {
      const apiGroups = await API.graphql(graphqlOperation(queries.getNtig, { PK: "Group#" + group, SK: "Officers#2" }));

      let listItem = await apiGroups.data.getNTIG;
      let PK = await apiGroups.data.getNTIG.PK.split("#")[1];
      let secJson = await JSON.parse(apiGroups.data.getNTIG.Secretary);
      let id = 1;

      let receivedItems = {
        Id: id,
        PK: PK,
        SecretaryName: secJson.Name,
      };

      setList(receivedItems);
    } catch (err) {}
  }
  useEffect(() => {
    if (!isAuthenticated) {
      return;
    }
    getData(group);
  }, [isAuthenticated, group]);
  console.log(list);
  return list;
}

What I'm hoping someone can help me with is either correcting the logic so the setting the state value works, or an alternative to changing the form input value.我希望有人可以帮助我更正逻辑以便设置 state 值有效,或者更改表单输入值的替代方法。

I'm not sure if this is the correct way to do this, but it works and it also resolves an additional problem of changing an uncontrolled element, when the element is re-rendered, when the state value changes from the API call.我不确定这是否是正确的方法,但它有效,并且它还解决了更改不受控制元素的附加问题,当重新渲染元素时,当 state 值从 API 调用更改时。

This is where I get the values from the API call and initally setState:这是我从 API 调用和初始 setState 中获取值的地方:

 const dbContact = useDbContact(contact.Group);
 const [dbSecName, setDbSecName] = useState("");

I then use useEffect with an async function to wait for the value from the API call and setState using that value:然后我使用 useEffect 和异步 function 来等待来自 API 调用的值,并使用该值设置状态:

useEffect(() => {
    async function getDbContact() {
        let secName = await dbContact.SecretaryName;
        setDbSecName(secName);
        }
    getDbContact();
}, [dbContact.SecretaryName]);

I can then use the value in the form input:然后我可以使用表单输入中的值:

<Form.Control type="input" name="2secname" value={dbSecName || ""} readOnly />

Obviously I need to handle the state change in the form input but that should now be straight forward.显然,我需要处理表单输入中的 state 更改,但现在应该直截了当。

I'm no developer so if a better answer comes along I'd be pleased to see it.我不是开发人员,所以如果有更好的答案出现,我会很高兴看到它。 For now, I seem to have resolved my own problem.现在,我似乎已经解决了我自己的问题。

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

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