question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Possible race condition leading to "TypeError: Cannot read property 'context' of undefined"

See original GitHub issue

Describe 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:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
marvinhagemeistercommented, Feb 13, 2022

@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 includes preact.mjs (ESM Module) and preact.js (CommonJS Module).

Reported it upstream at https://github.com/vitest-dev/vitest/issues/747

1reaction
dancrascommented, Feb 11, 2022

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

  • Error only occurs with useContext. Using Context.Consumer works as expected
  • Error occurs in vitest but not jest so likely some race condition as mentioned above
TypeError [Error]: Cannot read properties of null (reading 'context')
    at Proxy.exports.useContext (/app/node_modules/preact/hooks/dist/hooks.js:1:2437)
    at d.ExampleConsumer [as constructor] (/app/app/src/preact-bug.test.jsx:38:41)
    at d.M [as render] (/app/node_modules/preact/dist/preact.js:1:7837)
    at j (file:///app/node_modules/preact/dist/preact.mjs:1:5752)
    at file:///app/node_modules/preact/dist/preact.mjs:1:1494
    at Array.some (<anonymous>)
    at g (file:///app/node_modules/preact/dist/preact.mjs:1:1392)
Read more comments on GitHub >

github_iconTop Results From Across the Web

Cannot read property 'context' of undefined with css-blocks ...
I am using create-react-app and I have ejected. I am trying to run the dev server and I get the message from my...
Read more >
Problem loading macros in the new editor. Getting 404 in ... - Jira
For some pages, there seems to be some race condition on load of js or connect js not binded ... TypeError: Cannot read...
Read more >
Changelog - Cypress Documentation
Fixed an issue with Angular Component Testing where urls within SASS/SCSS files were not being correctly resolved which could result in incomplete styling....
Read more >
React - Cannot read property 'map' of undefined - DEV Community ‍ ‍
Another possible solution is to conditionally render the items , meaning if we have the items then render them, else don't render (or...
Read more >
Process | Node.js v19.3.0 Documentation
The event should not be used as an equivalent to On Error Resume Next . Unhandled exceptions inherently mean that an application is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found