Fix suspense in useState and useReducer initializers #41
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.
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 theuseReducer
'sinit
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 agetCurrentValue
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