为什么在 React.js 中函数内部变量值没有更新?

huangapple go评论61阅读模式
英文:

why variable value is not updated inside function . in react js?

问题

I am sending a function as props in reactjs. I have one state "form submitted" or "not submitted". I am getting wrong output (console output) inside the function. don't know why? But outside function, I am getting the correct value.

here is my code: Link to CodeSandbox

const NestedComponent = () => {
  const { registerState, formState } = useHookFormContext();
  console.log(formState.isSubmitted, "outside");

  return (
    <>
      <CustomApi
        key="abc"
        options={[]}
        onChange={() => {
          console.log(formState.isSubmitted, "inside");
          console.log("sss");
        }}
      />
    </>
  );
};

Steps to reproduce the bug:

  • Run the application. See the console; it will show false, which is correct.
  • Click the submit button. See the console; it will show true, which is correct.
  • Now change the dropdown value. See the console; it will show false value, which is incorrect. Why is it showing a different value?

为什么在 React.js 中函数内部变量值没有更新?

Is it a closure issue?

英文:

I am sending a function as props in reactjs .I have one state "form submitted" or "not submitted". I am getting wrong output (console output) inside the function . don't know why ? But outside function I am getting correct value.

here is my code
https://codesandbox.io/s/cool-dawn-0uu62q?file=/src/nested.tsx

const NestedComponent = () =&gt; {
  const { registerState, formState } = useHookFormContext();
  console.log(formState.isSubmitted, &quot;outside&quot;);

  return (
    &lt;&gt;
      &lt;CustomApi
        key={&quot;abc&quot;}
        options={[]}
        onChange={() =&gt; {
          console.log(formState.isSubmitted, &quot;inside&quot;);
          console.log(&quot;sss&quot;);
        }}
      /&gt;
    &lt;/&gt;
  );
};

steps to reproduce bug

  • run application . see console it will show false which is correct.
  • Click submit button . see console it will show true which is correct.
  • Now change the dropdown value .see console it will show false value which is incorrect correct. why is showing different value

为什么在 React.js 中函数内部变量值没有更新?

is it closure issue ???

答案1

得分: 0

以下是翻译好的部分:

这个版本的mui-react-hook-form-plus已经修复了问题 - v1.3.5

忘记在每个渲染周期重新分配onChangeRef。因此,闭包获取了之前的值


+ const onChangeRef = React.useRef(onChange);
+ /**
+  * 我们在每次渲染时更新引用
+ */
+ onChangeRef.current = onChange;

示例:https://codesandbox.io/s/quizzical-nova-ur0rv8?file=/src/nested.tsx

英文:

Fixed with this release of mui-react-hook-form-plus - v1.3.5

Forgot to reassign the onChangeRef in each render cycle. Hence closure got the prev value


+ const onChangeRef = React.useRef(onChange);
+ /**
+  * we update the ref on every render
+  */
+ onChangeRef.current = onChange;

example: https://codesandbox.io/s/quizzical-nova-ur0rv8?file=/src/nested.tsx

huangapple
  • 本文由 发表于 2023年4月10日 23:29:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/75978431.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定