Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[bug?] inner form cause outter form submit #14456

Closed
xialvjun opened this issue Dec 18, 2018 · 2 comments
Closed

[bug?] inner form cause outter form submit #14456

xialvjun opened this issue Dec 18, 2018 · 2 comments

Comments

@xialvjun
Copy link

example: https://codesandbox.io/s/8plqn8mwl

I don't know if it's a bug or a feature. If code like this:

function Portal({ children }) {
  return ReactDOM.createPortal(children, document.body);
}

<form id="outter_form" onSubmit={e => xxx}>
  <Portal>
    <form id="inner_form" onSubmit={e => xxx}>
      <button type="submit">submit</button>
    </form>
  </Portal>
</form>

// then the inner_from onSubmit will cause the outter form submit...
// I have to add `if (e.target !== e.currentTarget) return;` in outter form's onSubmit to prevent it.

If it's a feature, feel free to close this issue. But I still hope you document it in reactjs.org

@muqg
Copy link

muqg commented Dec 18, 2018

React's Portals documentation does state that events propagate to parents as if the children are still part of the main component tree, even though they pop out of the html one. You can avoid this by calling e.stopPropagation therefore.

@xialvjun
Copy link
Author

Oh, yeah, I remembered e.preventDefault(), but forgot e.stopProgation()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants