Bug: inconsistent auto batching

This issue has been created since 2022-09-15.

I have to useEffects in my code. in first one I have two setState, the second one is in then of Promise.resolve.
and in the second useEffect I make a request by apollo client.

React version: 18.2.0

Steps To Reproduce

  1. rerun app to see different logs

Link to code example:
https://codesandbox.io/s/confident-http-m4fey5?file=/src/App.js:623-641

The current behavior

In some run the output of logs is:
image
and in some other run:
image

The expected behavior

I expected react to does not batch setState("bb") and setState("aa") at all and always logs like this:
image

but I don't know why react in some run batch two setStates and in some other run doesn't batch. I don't know there is any problem with apollo because with fetch react always doesn't batch these to setStates

LuBustos wrote this answer on 2022-09-19

Hey @amirqasemi74! I think the problem is that every fetch takes differents times so the logs are differents, if you try to make a promise and get the same data as the Apollo fetch, you will have your expected behavior. Anyways you can use a setTimeout if one promise takes more time than other.
Here is an example:

  const run_1 = async () => {
    setA("aa");
    await Promise.resolve().then(() => {
      setTimeout(() => {
        console.log("here");
        setB("bb");
      }, 50);
    });
  };

I hope I've helped you 😊

More Details About Repo
Owner Name facebook
Repo Name react
Full Name facebook/react
Language JavaScript
Created Date 2013-05-24
Updated Date 2022-09-26
Star Count 195293
Watcher Count 6655
Fork Count 40426
Issue Count 1113

YOU MAY BE INTERESTED

Issue Title Created Date Comment Count Updated Date
Unable to get canu to run on compute cluster 4 2022-07-23 2022-08-28
[NTR] toxic bronchiolitis 1 2022-08-10 2022-09-19
Tabs above bookmarks 3 2021-12-06 2022-07-14
User configurable cmp bindings 0 2022-03-04 2022-07-01
Cloud Compatibility of OpenELB 3 2022-05-18 2022-09-10
Support showing translated AppData 0 2020-02-10 2022-08-20
window.create() not available to extensions 0 2022-06-16 2022-09-01
`SomeType.from_pydantic(...)` does not call resolvers of underlying types 2 2022-08-10 2022-09-09
Possible improvement for packed_load_active/packed_load_active2 on AVX2 0 2022-04-06 2022-09-13
Split notes does not take everything 2 2021-10-29 2022-05-16
Django application can't startup with openxlpy 2 2021-03-15 2022-07-18
I can't find the path /etc/onlyoffice/documentserver/ ! 4 2020-07-23 2022-09-21
Change tx version to uint32 2 2021-03-06 2022-09-12
Unable to disable short break timer 0 2022-06-12 2022-09-02
Need let the stop page on the current Chrome at current desktop 0 2022-03-01 2022-09-19
could add worklist Function 0 2022-02-24 2022-09-02
EasyLoading.show 增加个超时时间 5 2020-02-29 2022-07-10
Yakyak fails to launch, creates multiple processes 12 2021-04-09 2022-09-14
Different t2/t3 Unit Prices 11 2021-11-12 2022-08-31
Outdated Interface Screenshots 1 2022-09-12 2022-09-06
Update KaTeX Version 4 2021-07-09 2022-07-30
Using a reverse proxy 5 2021-05-03 2022-08-30
Multiple slack endpoints 0 2022-04-26 2022-08-29
Detect when a game has been launched 1 2022-09-15 2022-09-23
Infinite loop in snap_usedef 1 2021-07-23 2022-09-27
X68000 Aquales : label of floppies 2 2021-11-23 2022-08-25
Add Analytics API 1 2019-01-03 2022-01-16
Make openmp a package 6 2021-11-28 2022-08-28
Chaos mesh network interruptions doesn't work with Cilium Service Mesh 1 2022-05-26 2022-08-15
Error in using --format=wpapsk-opencl in john/kernels/opencl_cmac.h 2 2022-09-19 2022-09-25
Missing function definition 2 2022-09-19 2022-09-25
Address behavior of an open session when the endpoint becomes unresponsive 2 2022-07-05 2022-09-13
May also need extra user information but not just the token 4 2020-04-20 2022-09-26
MvpNullObjectBasePresenter + R8 = ClassCastException 2 2019-07-19 2022-09-17
Bug with backing out of Search screen 0 2021-03-17 2022-09-26
Redirect the landing page `http://127.0.0.1:4000/` to `http://127.0.0.1:4000/library_name` 1 2021-12-11 2022-09-03
uk_UA address provider - fix streets and cities names, added regions 1 2021-08-03 2021-11-30
[BUG] useForm.js存在逻辑处理的问题 2 2021-12-11 2022-09-23
Update Forseti to use latest version of Config Validator 0 2020-08-17 2022-09-01
Add change directory flag to instruction in Guide 0 2022-04-14 2022-09-17
Networking breaks in some corner-cases [API-1020] 1 2021-12-01 2022-09-11
Add support for TS_NODE_TRANSPILER environment variable 0 2022-07-17 2022-09-18
Failed to parse a heredoc expression as it is 1 2021-01-05 2022-09-19
Quality report mail: maintenance effictivenes seems to use still an old (wrong) calculation 3 2022-01-11 2022-09-03
[libbeat] ILM writes should use `require_alias=true` 6 2021-09-10 2022-08-14
Optimize the indices route that populates the index threshold and es query rule index picker#69 1 2022-08-04 2022-08-05
[Lens] Bar chart with very long tooltips seem to "dance" while hovering on them 6 2022-01-12 2022-07-16
Data type mismatch for horizontal axis, use a different function error message on lens doesn't help the user to resolve the error 5 2022-08-04 2022-08-05
Bug: Wrong warning: unmountComponentAtNode(): The node you're attempting to unmount was rendered by another copy of React. 6 2021-09-17 2022-09-21
Address obsolete ContentItemGroup.FindItems in PackageValidation 4 2022-01-06 2022-09-03