Possible race condition leading to "TypeError: Cannot read property 'context' of undefined"
See original GitHub issueDescribe the bug
There seems to be a race condition related to useContext
with Suspense
and preact-ssr-prepass
. In my application i use usePromise
(from react-promise-suspense
) to await on certain computation server-side.
Sometimes (but not systematically) the ssr prepass fails with this error :
error TypeError: Cannot read property 'context' of undefined
at Object.exports.useContext (.../frugal/node_modules/preact/hooks/src/index.js:239:36)
at Object.useAssertCreator (.../frugal/packages/preact-render/src/asset.ts:8:32)
at Object.useImage (.../frugal/packages/preact-render/src/useImage.ts:8:26)
at d.call (.../frugal/packages/ulule/src/pages/project/descriptor.tsx:50:20)
at t (.../frugal/node_modules/preact-ssr-prepass/src/index.js:82:22)
If i remove the longest computations (the ones that would need to be awaited on the most during the ssr prepass), the error disappears. If i replace the computation with a simple timeout, the error still happens. That’s why i suspect a race condition. I don’t know if this is an issue with preact
or preact-ssr-prepass
.
To Reproduce I could not reproduce this issue with a simplified setup on CodeSandbox. I could share my codebase if needed but there is a lot of moving parts.
Expected behavior
When a value is passed to a context.Provider
, a useContext
should return this value even in the context of an ssr prepass.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:5 (2 by maintainers)
Top GitHub Comments
@dancras Looking at that output it seems like
vitest
is including two different versions of Preact. Hooks require by design a singleton and multiple Preact versions don’t know about each other. That’s why the context value is missing and the error only pops up with hooks. Here in particular it includespreact.mjs
(ESM Module) andpreact.js
(CommonJS Module).Reported it upstream at https://github.com/vitest-dev/vitest/issues/747
I think I’ve just encountered this same error in a different context. I’ve managed to narrow it down to a very simple failing example: https://github.com/dancras/preact-bug
useContext
. UsingContext.Consumer
works as expectedvitest
but notjest
so likely some race condition as mentioned above