反应协调会导致意想不到的结果

发布于 2025-01-14 17:49:24 字数 1556 浏览 2 评论 0原文

您好,我有一个关于反应协调的问题。

这是我的代码

const Foo = () => {
   const [buttonId, setButtonId] = useState(1);

   const handleClick = (e: React.MouseEvent) => {

      // when I uncomment below line, submit is blocked.
      // e.preventDefault();

      // when I comment below line, form submission not occurred. 
      // it makes sense because Btn2 Element doesn't exist never anymore 
      setButtonId(2);
   };

   const handleSubmit = () => {
      // when click Btn 1, handleSubmit is called and printed 'submit!'
      console.log('submit!');
   };

   return (
      <form onSubmit={handleSubmit}>
         <input type="text" name="dummy" value="dummy" />

         // Why is the form submitted when I clicked on Btn 1? 
         {buttonId === 1 ? (
           <button type="button" onClick={handleClick}>Btn 1</button>
         ) : (
           <button type="submit">Btn 2</button>
         )}
      </form>
   )
};

当我单击button1时,即使button1没有type="submit",也会处理表单提交。

更奇怪的是,当我在 Button1 onClick 处理程序中取消注释 e.preventDefault() 并单击它时,不会发生表单提交。

为什么处理点击事件(handleclick)的处理程序绑定到下一个按钮的提交事件中涉及的第一个按钮?反应协调的结果是否符合预期且正确?

我猜测是在对账过程中更新元素类型的过程中出现的问题。

我已经知道为每个按钮注册不同的按键可以解决这个问题。但我想知道为什么会发生这种情况。

谁能给我一个清晰的想法?

请查看此链接:https://codesandbox.io /s/ecstatic-wozniak-s7r7rq?file=/src/App.js

hi I have a question about react reconciliation.

here's my code

const Foo = () => {
   const [buttonId, setButtonId] = useState(1);

   const handleClick = (e: React.MouseEvent) => {

      // when I uncomment below line, submit is blocked.
      // e.preventDefault();

      // when I comment below line, form submission not occurred. 
      // it makes sense because Btn2 Element doesn't exist never anymore 
      setButtonId(2);
   };

   const handleSubmit = () => {
      // when click Btn 1, handleSubmit is called and printed 'submit!'
      console.log('submit!');
   };

   return (
      <form onSubmit={handleSubmit}>
         <input type="text" name="dummy" value="dummy" />

         // Why is the form submitted when I clicked on Btn 1? 
         {buttonId === 1 ? (
           <button type="button" onClick={handleClick}>Btn 1</button>
         ) : (
           <button type="submit">Btn 2</button>
         )}
      </form>
   )
};

When I click button1, form submission is processed even though button1 don't have type="submit".

What's even weirder is that when I uncomment e.preventDefault() in button1 onClick handler and click it, form submission don't happened.

Why is the handler that handling the click event(handleclick) bounded to the first button involved in the submission event of the next button? Is it expected and right result in react reconciliation?

I guess that it is a problem that occurs in the process of updating the type of element during the reconciliation process.

I already know that registering different keys to each button can solve this issue. but I want to know why this happens.

Who's gonna give me a clear thought?

Please check out this link: https://codesandbox.io/s/ecstatic-wozniak-s7r7rq?file=/src/App.js

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

醉生梦死 2025-01-21 17:49:24

尝试向 button 元素添加一个键,以便 React 知道这些是不同的元素

    <form onSubmit={handleSubmit}>
      <input type="text" name="dummy" value="dummy" />

      {buttonId === 1 ? (
        <button key="1" type="button" onClick={handleClick}>
          Btn 1
        </button>
      ) : (
        <button key="2" type="submit">Btn 2</button>
      )}
    </form>

Try adding a key to the button element, so React knows that those are different elements

    <form onSubmit={handleSubmit}>
      <input type="text" name="dummy" value="dummy" />

      {buttonId === 1 ? (
        <button key="1" type="button" onClick={handleClick}>
          Btn 1
        </button>
      ) : (
        <button key="2" type="submit">Btn 2</button>
      )}
    </form>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文