Skip to content

fix(useTable): work around React 18 useReducer eager bailout change #4207

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

Merged
merged 1 commit into from
Jul 22, 2022

Conversation

henryqdineen
Copy link

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: facebook/react#22445
Codesandbox repro: https://codesandbox.io/s/cool-edison-45tkqx?file=/src/App.js:1331-1358

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: facebook/react#22445
Codesandbox repro: https://codesandbox.io/s/cool-edison-45tkqx?file=/src/App.js:1331-1358
@tannerlinsley tannerlinsley merged commit 06703a5 into TanStack:v7 Jul 22, 2022
@henryqdineen henryqdineen deleted the fix-v7-react-18-compat branch July 22, 2022 20:29
@bug-brain
Copy link

Is there any chance this change can be published? The problem can also lead to Cannot update a component (`X`) while rendering a different component (`Y`). warnings.

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

Successfully merging this pull request may close these issues.

3 participants