简体   繁体   English

重新渲染时反应自定义挂钩 state 未更新

[英]react custom hook state is not updating when re-rendering

I'm having an issue when repopulating form for edit using react hooks.使用反应挂钩重新填充表单以进行编辑时遇到问题。

Parent Component: Edit.js父组件:Edit.js

const EditData = (props) => {
    const { Id } = props.match.params;
    const dispatch = useDispatch();
    // calling redux action to get the data
    useEffect(() => {
      dispatch(getDataById(Id));
    }, [Id]);
    const data = useSelector((state) => state.data);
    const initialState = {
      Id: data.cardId || '',
      Number: data.Number || '',
      Date: data.Date,
    };
    //calling custom hook
    const { handleChange, handleSubmit, values,errors } = useForm(
        initialState,// passing initial state to custom hook
        validateOnSubmit,
        submit
    );
    // used to submit the data
    function submit() {
      dispatch(updateCard(values));
    }
    return (<DateForm
            handleSubmit={handleSubmit}
            handleChange={handleChange}
            values={values}
            />);
  };

Custom hook: useform.js自定义钩子:useform.js

  const useForm = (initialState, validateOnSubmit, callback) => {
    const [values, setValues] = useState(initialState);
    const [errors, setErrors] = useState({});
    const [isSubmitting, setIsSubmitting] = useState(false);

    const handleChange = (event) => {
      const { name, value } = event.target;
      setValues({
        ...values,
        [name]: value
      });
    };
    const handleSubmit = (event) => {
      event.preventDefault();
      setErrors(validateOnSubmit(values));
      setIsSubmitting(true);
    };

    useEffect(() => {
      if (Object.keys(errors).length === 0 && isSubmitting) {
        callback();
      }
    }, [errors]);

    return {
      handleChange,
      handleSubmit,
      values,
      errors
    };
  };

when the API call get finished the react re-render the custom but the local state of the hook is not updating.当 API 调用完成时,反应重新渲染自定义,但钩子的本地 state 没有更新。

const useForm = (initialState, validateOnSubmit, callback) => {
      console.log(initialState);

on second render, here i can receive data from the API在第二次渲染时,我可以从 API 接收数据

    const [values, setValues] = useState(initialState);

but values is not getting updated, values is still holding the state from the initial render但是没有得到更新,仍然保持初始渲染中的 state

I cannot figure out why this is.我不知道为什么会这样。 I'm just started to use react hooks, please help me.我刚开始使用反应钩子,请帮助我。

As OP stated in a comment:正如OP在评论中所说:

the initialState variable is updated when the API call get completed,I'm passing that initialState variable to const [values, setValues] = useState(initialState);当 API 调用完成时,initialState 变量被更新,我将该 initialState 变量传递给 const [values, setValues] = useState(initialState); , so it should update the values variable right?. ,所以它应该更新 values 变量吗? but it's not!但事实并非如此!

It is should update the state, the initial state is assigned once until the component unmounts.应该更新 state,初始 state 分配一次,直到组件卸载。

See useState API , it stated in lazy initialization :请参阅useState API ,它在延迟初始化中说明:

The initialState argument is the state used during the initial render. initialState参数是在初始渲染期间使用的 state。 In subsequent renders, it is disregarded.在随后的渲染中,它被忽略。

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

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