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

Guarantees about re-rendering and setState batching inside an event handler #3719

Open
NullPainter2 opened this issue Jun 2, 2021 · 2 comments

Comments

@NullPainter2
Copy link

Hi,
I have React 17 and I am using hooks. I believe answer is hinted by "Multiple updates inside event handlers get batched into a single update." ... https://reactjs.org/docs/implementation-notes.html#what-we-left-out , but I am not 100% sure I understand it correctly.

Does React guarantee that while my event handler is being processed no re-render happens? That is first re-render happens after my event handler exits.

  const onClick = () => {
    setState1(1);
    // do some stuff
    setState1(2);
    // do some stuff
    setState4(4);

    // no-rerender will ever be called by React until this point (that is until function returns) ???
    // re-render is only scheduled to happen ASAP
  } 

Thanks for answering :)

@gaearon
Copy link
Member

gaearon commented Jun 2, 2021

yes

@myteax
Copy link

myteax commented Jun 2, 2021

Yes that is correct. However, you pass the event as a parameter and use event.preventDefault() to complete prevent it from re-rendering if you want.

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

3 participants