reactjs / reactjs/react.dev

useReducer dispatch flow clarification

Open
#2,017 0 comments 1 reaction 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've been looking around to try to find out more about the "dispatch / render" flow and can not find anything relevant yet.

My question is: when I do 2 dispatchsequentially such as:

dispatch({type: ACTION_1});
dispatch({type: ACTION_2});

Are we sure that the 2 actions will be processed before the next rendering cycle occurs?
Could it be that on the next rendering cycle, only ACTION_1 has been processed then ACTION_2 is processed on a later rendering cycle?

In other words, from the react perspective, is doing:

dispatch({type: ACTION_1});
dispatch({type: ACTION_2});

equivalent to:

dispatch({type: ACTIONS: payload: [ACTION_1, ACTION2]});

In a small demo, it seems that it doesn't matter:
https://codesandbox.io/s/tzp0s (see the console messages)

In my larger react app, it seems that often time there is a render cycle between 2 consecutive dispatch.

Is it considered an anti-pattern to do 2 dispatch sequentially and should we instead do 1 dispatch with the content of the 2 dispatch combined?

Thank you!

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

No documentation file or test is named. Begin by reproducing the sequential dispatch behavior in the linked CodeSandbox and comparing it with the larger-app behavior described; done would require locating the relevant React guidance and clarifying the dispatch/render relationship and recommended usage.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.