reactjs / reactjs/react.dev

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

Open
#3,719 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
11.8k
Forks
7.9k
Avg merge
1d 11h
Merged PRs (30d)
11

Description

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 :)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading the linked React implementation notes and checking the React 17 event-handler batching behavior described in the question. Determine whether the documentation should state a guarantee about re-render timing and setState batching, then update the relevant documentation once the intended wording and scope are confirmed.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, react
Domain
documentation
Issue type
Documentation
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.