简体   繁体   English

使用 React Hooks 返回动态创建的嵌套 object 的先前 state?

[英]Return the previous state of a dynamically created nested object, using React Hooks?

I have a state object that I would like to dynamically create a key and an object inside the key.我有一个 state object 我想动态创建一个密钥和一个 object 在密钥内。 It needs to be an object so it is dynamic for the parent components and return the previous state.它必须是 object,因此它对于父组件是动态的,并返回之前的 state。 The Key is dynamic based off the parent component. Key是基于父组件的动态的。 The setState as of right now works as expected creating the dynamic parts of the state however, returning the state inside the nested object is where i'm confused. setState现在按预期工作,创建 state 的动态部分,但是,在嵌套的 object 中返回 state 是我感到困惑的地方。 The state as of right now overrides the previous state. state 现在覆盖了之前的 state。

Here is some code...这是一些代码...

import React, { useState, useCallback } from "react";
import { FormGroup, CustomInput } from "reactstrap";
import UniqueString from "unique-string";

function Ratings({ parentCheckedGood, parentCheckedNA, question, section }) {
  const [state, setState] = useState({});
  //THE ISSUE IS INSIDE THIS ONCHANGE FUNCTION
  const onChange = useCallback(
    (e) => {
      const { name, value } = e.target;
      // The state structure works as expected but getting the previous nested state is the issue
      setState((state) => ({
        ...state,
        [section]: { ...state[section], [question]: value },
      }));
    },
    [section, question]
  );
  console.log(state);
  const string = UniqueString();
  return (
    <div>
      <FormGroup required>
        <div>
          <CustomInput
            // checked={parentCheckedGood}
            onChange={onChange}
            type="radio"
            id={string + "1"}
            name={question}
            value="Good"
            label="Good"
          />
          <CustomInput
            onChange={onChange}
            type="radio"
            id={string + "2"}
            name={question}
            value="Fair"
            label="Fair"
          />
          <CustomInput
            onChange={onChange}
            type="radio"
            id={string + "3"}
            name={question}
            value="Poor"
            label="Poor"
          />
          <CustomInput
            // checked={parentCheckedNA}
            onChange={onChange}
            name={question}
            type="radio"
            id={string + "4"}
            value="N/A"
            label="N/A"
          />
        </div>
      </FormGroup>
    </div>
  );
}

export default Ratings;

Edit:编辑:

For anyone needing an example of the state.对于任何需要 state 示例的人。 The state structure looks like this: This is just an example, you can't access it with dot notation. state 结构如下所示:这只是一个示例,您不能使用点符号访问它。

state = {
    section: {name: value}
  }

Answer:回答:

After doing some debugging I found out that what I had was the correct way to do it.在做了一些调试之后,我发现我所拥有的是正确的方法。 I had another onChange function in the parent that was conflicting with the setState.我在与 setState 冲突的父级中有另一个 onChange function。 If anyone is looking for a dynamic way to create a state this is probably the best way to do it.如果有人正在寻找创建 state 的动态方法,这可能是最好的方法。 Without creating extra setStates.无需创建额外的 setStates。

all customInput share the same state;所有 customInput 共享相同的 state; if childen component return the state which has the same key,they will overide the previous;如果子组件返回具有相同密钥的 state,它们将覆盖前一个;

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

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