Skip to content
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

Fix suspense in useState and useReducer initializers #41

Merged
merged 2 commits into from
Feb 19, 2020

Conversation

kitten
Copy link
Contributor

@kitten kitten commented Feb 19, 2020

Fix FormidableLabs/next-urql#34

This fixes an edge case where a suspense is triggered in the useState initialiser (useState(() => throw Promise.resolve())) or in the useReducer's init argument (useReducer(x, y, () => throw Promise.resolve()))

This is achieved by ensuring that the hook's state is initialised before applying updates on a rerender, since the workInProgressHook will have already been created after suspense.

And subsequently we also fix the initialiser not being called on rerenders by instead checking whether the hook's state has been initialised.

Example: This may happen when useSubscription is used with a getCurrentValue function that may trigger suspense by throwing a promise, See: https://github.com/facebook/react/blob/4d9f8500651c5d1e19d8ec9a2359d5476a53814b/packages/use-subscription/src/useSubscription.js#L33-L37

- Apply initial state if hook is uninitialised
- Ensure that queue/dispatch are initialised before applying updates
@kitten kitten changed the title Fix/suspense in state initialiser Fix suspense in useState and useReducer initializers Feb 19, 2020
@FormidableLabs FormidableLabs deleted a comment from codecov-io Feb 19, 2020
@kitten kitten merged commit e7efc33 into master Feb 19, 2020
@kitten kitten deleted the fix/suspense-in-state-initialiser branch February 19, 2020 11:55
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.

runtime error with urql@1.9.0 + react-ssr-prepass
2 participants