GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Remove usereducer eager bailout by josephsavona · Pull Request #22445 · react/react

Conversation

@josephsavona

@josephsavona josephsavona commented Sep 27, 2021 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

Remove eager bailout mechanism for useReducer

Both useState and userReducer have an eager bailout mechanism that attempts to avoid a re-render by checking if the state/reducer update is a no-op. Per @acdlite there are some flaws with the eager bailout mechanism for useReducer specifically, so this PR removes the eager bailout for useReducer and keeps it for useState.

Changes:

  • Forked dispatchAction() into dispatchReducerAction() and dispatchSetState(), updated call sites to use one of the new names.
  • Removed the eager bailout mechanism from dispatchReducerAction().
  • Remove eagerReducer from the Update type, replacing it with hasEagerState to tell if there's an eager state update.

How did you test this change?

Followed the contribution guidelines: yarn test/lint/flow

Closes #15088
Closes #21419
Closes #21416
Closes #17953

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

hosseini44444, vishwasrvalke, and peet reacted with heart emoji dai-shi and inokawa reacted with eyes emoji
All reactions
  • ❤️ 3 reactions
  • 👀 2 reactions
josephsavona added 3 commits September 27, 2021 13:21
@josephsavona
Fork dispatchAction for useState/useReducer
11a379a
@josephsavona
Remove eager bailout from forked dispatchReducerAction, update tests
27048d2
@josephsavona
Update eager reducer/state logic to handle state case only
882bbad
@josephsavona
josephsavona requested a review from acdlite September 27, 2021 20:59
@josephsavona
sync reconciler fork
e49a79a
@facebook-github-bot facebook-github-bot added the CLA Signed label Sep 27, 2021
acdlite
acdlite reviewed Sep 27, 2021
View reviewed changes
Comment thread packages/react-reconciler/src/__tests__/ReactHooksWithNoopRenderer-test.js Outdated
});

// TODO: delete now that we removed the eager bailout optimization? or do we want to keep this
// around in some capacity?

@acdlite acdlite Sep 27, 2021

Copy link
Copy Markdown
Collaborator

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah could be useful as a way to document the current behavior, or in case we ever add it back. You can retitle to "useReducer does not eagerly bail out of state updates."

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

josephsavona reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
acdlite
acdlite approved these changes Sep 27, 2021
View reviewed changes

@acdlite acdlite left a comment

Copy link
Copy Markdown
Collaborator

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! Good to merge assuming CI passes

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@josephsavona
rename test
bf21962
@gaearon

gaearon commented Sep 27, 2021

Copy link
Copy Markdown
Collaborator

Does this make tests from #15198 pass?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@gaearon

gaearon commented Sep 27, 2021

Copy link
Copy Markdown
Collaborator

Another possible regression test that may be worth including is in #21419

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@josephsavona
test cases from react#15198
c01afe4
@josephsavona

josephsavona commented Sep 27, 2021

Copy link
Copy Markdown
Contributor Author

Does this make tests from #15198 pass?

@gaearon Yup! I just added them.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@gaearon

gaearon commented Sep 27, 2021

Copy link
Copy Markdown
Collaborator

Did you verify they failed on main? Just to make sure this is actually the fix for that problem

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

josephsavona added 2 commits September 27, 2021 15:00
@josephsavona
comments on new test cases
b6cabe8
@josephsavona
comments on new test cases
895e2a7
@josephsavona

josephsavona commented Sep 27, 2021

Copy link
Copy Markdown
Contributor Author

Did you verify they failed on main? Just to make sure this is actually the fix for that problem

Oops no - i went back and checked and the first new test case fails on main.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

josephsavona
josephsavona commented Sep 27, 2021
View reviewed changes
Comment thread packages/react-reconciler/src/__tests__/ReactHooksWithNoopRenderer-test.js Outdated
expect(ReactNoop).toMatchRenderedOutput('0');
});

it('useReducer should apply potential no-op changes if made relevant by other updates in the batch', () => {

@josephsavona josephsavona Sep 27, 2021

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This passed on main, but it seems like a good test of priority of eager bailout vs updates from a parent.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

gaearon reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
josephsavona
josephsavona commented Sep 27, 2021
View reviewed changes
Comment thread packages/react-reconciler/src/__tests__/ReactHooksWithNoopRenderer-test.js
});
expect(Scheduler).toHaveYielded([
'Render disabled: false',
'Render count: 0',

@josephsavona josephsavona Sep 27, 2021

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this failed on main, with 'Render count: 3'

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

gaearon, Aprillion, and exposir reacted with thumbs up emoji
All reactions
  • 👍 3 reactions
@josephsavona
test case from react#21419
1e621bf
@josephsavona

josephsavona commented Sep 27, 2021

Copy link
Copy Markdown
Contributor Author

I confirmed that the test from #21419 repros on main and is fixed by this change

gaearon reacted with heart emoji
All reactions
  • ❤️ 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@josephsavona
minor tweak to test name to kick CI
432cf94
@josephsavona

josephsavona commented Sep 27, 2021

Copy link
Copy Markdown
Contributor Author

@acdlite, @gaearon any idea why CI is failing with "Error: Could not find commit for: 1e621bf"?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@acdlite

acdlite commented Sep 27, 2021 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Collaborator

The CodeSandbox CI job has been a bit flaky lately, I'll look into that. It's not merge blocking, though.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@acdlite
acdlite merged commit 6638815 into react:main Sep 27, 2021
@acdlite

acdlite commented Sep 27, 2021

Copy link
Copy Markdown
Collaborator

Thanks for working on this, @josephsavona!

josephsavona and Aprillion reacted with thumbs up emoji
All reactions
  • 👍 2 reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@acdlite acdlite mentioned this pull request Sep 28, 2021
Extract queueing logic into shared functions #22452
Merged
acdlite added a commit that referenced this pull request Sep 28, 2021
@acdlite
Extract queueing logic into shared functions (#22452)
d9fb383
As a follow up to #22445, this extracts the queueing logic that is
shared between `dispatchSetState` and `dispatchReducerAction` into
separate functions. It likely doesn't save any bytes since these will
get inlined, anyway, but it does make the flow a bit easier to follow.
facebook-github-bot pushed a commit to react/react-native that referenced this pull request Oct 12, 2021
Andrei Shikov @facebook-github-bot
React Native sync for revisions e8feb11...afcb9cd
c75ad2c
Summary:
This sync includes the following changes:
- **[579c008](react/react@579c008a7 )**: [Fizz/Flight] pipeToNodeWritable(..., writable).startWriting() -> renderToPipeableStream(...).pipe(writable) ([#22450](react/react#22450)) //<Sebastian Markbåge>//
- **[f2c3811](react/react@f2c381131 )**: fix: useSyncExternalStoreExtra ([#22500](react/react#22500)) //<Daishi Kato>//
- **[0ecbbe1](react/react@0ecbbe142 )**: Sync hydrate discrete events in capture phase and dont replay discrete events ([#22448](react/react#22448)) //<salazarm>//
- **[a724a3b](react/react@a724a3b57 )**: [RFC] Codemod invariant -> throw new Error ([#22435](react/react#22435)) //<Andrew Clark>//
- **[201af81](react/react@201af81b0 )**: Release pooled cache reference in complete/unwind ([#22464](react/react#22464)) //<Joseph Savona>//
- **[033efe7](react/react@033efe731 )**: Call get snapshot in useSyncExternalStore server shim ([#22453](react/react#22453)) //<salazarm>//
- **[7843b14](react/react@7843b142a )**: [Fizz/Flight] Pass in Destination lazily to startFlowing instead of in createRequest ([#22449](react/react#22449)) //<Sebastian Markbåge>//
- **[d9fb383](react/react@d9fb383d6 )**: Extract queueing logic into shared functions ([#22452](react/react#22452)) //<Andrew Clark>//
- **[9175f4d](react/react@9175f4d15 )**: Scheduling Profiler: Show Suspense resource .displayName ([#22451](react/react#22451)) //<Brian Vaughn>//
- **[eba248c](react/react@eba248c39 )**: [Fizz/Flight] Remove reentrancy hack ([#22446](react/react#22446)) //<Sebastian Markbåge>//
- **[6638815](react/react@66388150e )**: Remove usereducer eager bailout ([#22445](react/react#22445)) //<Joseph Savona>//
- **[d3e0869](react/react@d3e086932 )**: Make root.unmount() synchronous  ([#22444](react/react#22444)) //<Andrew Clark>//
- **[2cc6d79](react/react@2cc6d79c9 )**: Rename onReadyToStream to onCompleteShell ([#22443](react/react#22443)) //<Sebastian Markbåge>//
- **[c88fb49](react/react@c88fb49d3 )**: Improve DEV errors if string coercion throws (Temporal.*, Symbol, etc.) ([#22064](react/react#22064)) //<Justin Grant>//
- **[05726d7](react/react@05726d72c )**: [Fix] Errors should not "unsuspend" a transition ([#22423](react/react#22423)) //<Andrew Clark>//
- **[3746eaf](react/react@3746eaf98 )**: Packages/React/src/ReactLazy ---> changing -1 to unintialized ([#22421](react/react#22421)) //<BIKI DAS>//
- **[04ccc01](react/react@04ccc01d9 )**: Hydration errors should force a client render ([#22416](react/react#22416)) //<Andrew Clark>//
- **[029fdce](react/react@029fdcebb )**: root.hydrate -> root.isDehydrated ([#22420](react/react#22420)) //<Andrew Clark>//
- **[af87f5a](react/react@af87f5a83 )**: Scheduling Profiler marks should include thrown Errors ([#22417](react/react#22417)) //<Brian Vaughn>//
- **[d47339e](react/react@d47339ea3 )**: [Fizz] Remove assignID mechanism ([#22410](react/react#22410)) //<Sebastian Markbåge>//
- **[3a50d95](react/react@3a50d9557 )**: Never attach ping listeners in legacy Suspense ([#22407](react/react#22407)) //<Andrew Clark>//
- **[82c8fa9](react/react@82c8fa90b )**: Add back useMutableSource temporarily ([#22396](react/react#22396)) //<Andrew Clark>//
- **[5b57bc6](react/react@5b57bc6e3 )**: [Draft] don't patch console during first render ([#22308](react/react#22308)) //<Luna Ruan>//
- **[cf07c3d](react/react@cf07c3df1 )**: Delete all but one `build2` reference ([#22391](react/react#22391)) //<Andrew Clark>//
- **[bb0d069](react/react@bb0d06935 )**: [build2 -> build] Local scripts //<Andrew Clark>//
- **[0c81d34](react/react@0c81d347b )**: Write artifacts to `build` instead of `build2` //<Andrew Clark>//
- **[4da03c9](react/react@4da03c9fb )**: useSyncExternalStore React Native version ([#22367](react/react#22367)) //<salazarm>//
- **[48d475c](react/react@48d475c9e )**: correct typos ([#22294](react/react#22294)) //<Bowen>//
- **[cb6c619](react/react@cb6c619c0 )**: Remove Fiber fields that were used for hydrating useMutableSource ([#22368](react/react#22368)) //<Sebastian Silbermann>//
- **[64e70f8](react/react@64e70f82e )**: [Fizz] add avoidThisFallback support ([#22318](react/react#22318)) //<salazarm>//
- **[3ee7a00](react/react@3ee7a004e )**: devtools: Display actual ReactDOM API name in root type ([#22363](react/react#22363)) //<Sebastian Silbermann>//
- **[79b8fc6](react/react@79b8fc667 )**: Implement getServerSnapshot in userspace shim ([#22359](react/react#22359)) //<Andrew Clark>//
- **[86b3e24](react/react@86b3e2461 )**: Implement useSyncExternalStore on server ([#22347](react/react#22347)) //<Andrew Clark>//
- **[8209de2](react/react@8209de269 )**: Delete useMutableSource implementation ([#22292](react/react#22292)) //<Andrew Clark>//

Changelog:
[General][Changed] - React Native sync for revisions e8feb11...afcb9cd

jest_e2e[run_all_tests]

Reviewed By: yungsters

Differential Revision: D31541359

fbshipit-source-id: c35941bc303fdf55cb061e9996200dc868a6f2af
@markerikson markerikson mentioned this pull request Oct 22, 2021
[Early Preview] New React Documentation reactjs/react.dev#3965
Merged
@gaearon gaearon mentioned this pull request Mar 29, 2022
Add React 18 changelog #24195
Merged
zhengjitf pushed a commit to zhengjitf/react that referenced this pull request Apr 15, 2022
@josephsavona @zhengjitf
Remove usereducer eager bailout (react#22445)
d7a050c
* Fork dispatchAction for useState/useReducer

* Remove eager bailout from forked dispatchReducerAction, update tests

* Update eager reducer/state logic to handle state case only

* sync reconciler fork

* rename test

* test cases from react#15198

* comments on new test cases

* comments on new test cases

* test case from react#21419

* minor tweak to test name to kick CI
zhengjitf pushed a commit to zhengjitf/react that referenced this pull request Apr 15, 2022
@acdlite @zhengjitf
Extract queueing logic into shared functions (react#22452)
7cbc261
As a follow up to react#22445, this extracts the queueing logic that is
shared between `dispatchSetState` and `dispatchReducerAction` into
separate functions. It likely doesn't save any bytes since these will
get inlined, anyway, but it does make the flow a bit easier to follow.
@markerikson markerikson mentioned this pull request May 28, 2022
Bug: useReducer and same state in React 18 #24596
Closed
henryqdineen added a commit to henryqdineen/table that referenced this pull request Jul 22, 2022
@henryqdineen
fix(useTable): replace useReducer usage to workaround React 18 eager …
d101ff2
…bailout change

React 18 removed some logic from `useReducer()` where a dispatch would eagerly call reducers to recompute the state so now reducers will be called after render. This poses a problem for `useTable()` because during render it mutates the `instance` by spreading in `props` and does not expect this to happen before reducer execution. We were passing a `pageCount: undefined` prop to `useTable()` when the reducer executes `instance.pageCount` is been overwritten, causing a bug in our app. In the Codesandbox reproduction you can see that hitting the "next" button will not work. I understand that our abstraction around `useTable()` probably shouldn't be passing this prop when `undefined` but the behavior change was unexpected and I wonder if it breaks any other assumptions that this library makes. The fix I made here is to just replace `useReducer` with `useState`, which still has the eager bailout check.

React PR in question: react/react#22445
Codesandbox repro: https://codesandbox.io/s/cool-edison-45tkqx?file=/src/App.js:1331-1358
henryqdineen added a commit to henryqdineen/table that referenced this pull request Jul 22, 2022
@henryqdineen
fix(useTable): work around React 18 useReducer eager bailout change
33f3120
React 18 removed some logic from `useReducer()` where a dispatch would eagerly call reducers to recompute the state so now reducers will be called after render. This poses a problem for `useTable()` because during render it mutates the `instance` by spreading in `props` and does not expect this to happen before reducer execution. We were passing a `pageCount: undefined` prop to `useTable()` when the reducer executes `instance.pageCount` is been overwritten, causing a bug in our app. In the Codesandbox reproduction you can see that hitting the "next" button will not work. I understand that our abstraction around `useTable()` probably shouldn't be passing this prop when `undefined` but the behavior change was unexpected and I wonder if it breaks any other assumptions that this library makes. The fix I made here is to just replace `useReducer` with `useState`, which still has the eager bailout check.

React PR in question: react/react#22445
Codesandbox repro: https://codesandbox.io/s/cool-edison-45tkqx?file=/src/App.js:1331-1358
henryqdineen added a commit to henryqdineen/table that referenced this pull request Jul 22, 2022
@henryqdineen
fix(useTable): work around React 18 useReducer eager bailout change
a85d34e
React 18 removed some logic from `useReducer()` where a dispatch would eagerly call reducers to recompute the state so now reducers will be called after render. This poses a problem for `useTable()` because during render it mutates the `instance` by spreading in `props` and does not expect this to happen before reducer execution. We were passing a `pageCount: undefined` prop to `useTable()` and by the time the reducer executes `instance.pageCount` is been overwritten, causing a bug in our app. In the Codesandbox reproduction you can see that hitting the "next" button will not work. I understand that our abstraction around `useTable()` probably shouldn't be passing this prop when `undefined` but the behavior change was unexpected and I wonder if it breaks any other assumptions that this library makes. The fix I made here is to just replace `useReducer` with `useState`, which still has the eager bailout check.

React PR in question: react/react#22445
Codesandbox repro: https://codesandbox.io/s/cool-edison-45tkqx?file=/src/App.js:1331-1358
@henryqdineen henryqdineen mentioned this pull request Jul 22, 2022
fix(useTable): work around React 18 useReducer eager bailout change TanStack/table#4207
Merged
tannerlinsley pushed a commit to TanStack/table that referenced this pull request Jul 22, 2022
@henryqdineen
fix(useTable): work around React 18 useReducer eager bailout change (#…
06703a5
…4207)

React 18 removed some logic from `useReducer()` where a dispatch would eagerly call reducers to recompute the state so now reducers will be called after render. This poses a problem for `useTable()` because during render it mutates the `instance` by spreading in `props` and does not expect this to happen before reducer execution. We were passing a `pageCount: undefined` prop to `useTable()` and by the time the reducer executes `instance.pageCount` is been overwritten, causing a bug in our app. In the Codesandbox reproduction you can see that hitting the "next" button will not work. I understand that our abstraction around `useTable()` probably shouldn't be passing this prop when `undefined` but the behavior change was unexpected and I wonder if it breaks any other assumptions that this library makes. The fix I made here is to just replace `useReducer` with `useState`, which still has the eager bailout check.

React PR in question: react/react#22445
Codesandbox repro: https://codesandbox.io/s/cool-edison-45tkqx?file=/src/App.js:1331-1358
@hhkk28 hhkk28 mentioned this pull request Feb 23, 2023
Bug: useReducer rendering behaviour in React 18 #26131
Closed
@rickhanlonii rickhanlonii mentioned this pull request Feb 10, 2024
Bug: Parent rerenders unnecessarily on Child-to-Parent setter call, while child rerenders only when changes occur. #28287
Closed
@layershifter layershifter mentioned this pull request May 2, 2024
fix: useContextSelector with React 18 microsoft/fluentui#30951
Merged
@NMinhNguyen NMinhNguyen mentioned this pull request May 15, 2024
Bug: memory leak in useReducer #21692
Closed
@knaveenkumar3576 knaveenkumar3576 mentioned this pull request Jun 24, 2024
Faster Paint with delayed flush of Effects clauderic/dnd-kit#1435
Merged
@coderabbitai coderabbitai Bot mentioned this pull request Jun 3, 2026
feat(#57): Trace — mark trace points (working set + canvas feedback) CuriouslyCory/infinite-docs#84
Merged
10 tasks done
mrizwanashiq pushed a commit to mrizwanashiq/react that referenced this pull request Jun 25, 2026
@josephsavona
Remove usereducer eager bailout (react#22445)
39cd180
* Fork dispatchAction for useState/useReducer

* Remove eager bailout from forked dispatchReducerAction, update tests

* Update eager reducer/state logic to handle state case only

* sync reconciler fork

* rename test

* test cases from react#15198

* comments on new test cases

* comments on new test cases

* test case from react#21419

* minor tweak to test name to kick CI
mrizwanashiq pushed a commit to mrizwanashiq/react that referenced this pull request Jun 25, 2026
@acdlite
Extract queueing logic into shared functions (react#22452)
daef9db
As a follow up to react#22445, this extracts the queueing logic that is
shared between `dispatchSetState` and `dispatchReducerAction` into
separate functions. It likely doesn't save any bytes since these will
get inlined, anyway, but it does make the flow a bit easier to follow.
hanzo-dev pushed a commit to hanzoai/react that referenced this pull request Sep 9, 2026
@josephsavona
Remove usereducer eager bailout (react#22445)
76d88ee
* Fork dispatchAction for useState/useReducer

* Remove eager bailout from forked dispatchReducerAction, update tests

* Update eager reducer/state logic to handle state case only

* sync reconciler fork

* rename test

* test cases from react#15198

* comments on new test cases

* comments on new test cases

* test case from react#21419

* minor tweak to test name to kick CI
hanzo-dev pushed a commit to hanzoai/react that referenced this pull request Sep 9, 2026
@acdlite
Extract queueing logic into shared functions (react#22452)
5297d29
As a follow up to react#22445, this extracts the queueing logic that is
shared between `dispatchSetState` and `dispatchReducerAction` into
separate functions. It likely doesn't save any bytes since these will
get inlined, anyway, but it does make the flow a bit easier to follow.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@acdlite acdlite acdlite approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

CLA Signed

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Bug: useReducer, reducer function gets called twice (possible memory leak) Bug: useReducer runs the queued updates with new props useReducer - eagerReducer optimization discussion/questions

Uh oh!

There was an error while loading. Please reload this page.

4 participants

@josephsavona @gaearon @acdlite @facebook-github-bot
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.