Issue using Hooks with exported component
See original GitHub issueBug
When I render an imported component which uses the useContext
hook, I get the following error:
Uncaught TypeError: Cannot read property 'context' of undefined
at Module.useContext (index.js:173)
at d../src/example.tsx.exports.Example [as constructor] (example.tsx:7)
It appears that currentComponent
is undefined, but I’m not sure what that means or how to fix it.
I think the issue is related to my build process, so have created a repository which demonstrates the problem.
Steps to reproduce
- Clone https://github.com/oliverdunk/preact-bug
- Run
npm install
- Run
npm run-script run
Expected behaviour
The number 5 is rendered on the page, inside of a H1.
Actual behaviour
When the Example
component is rendered, the previously mentioned error is thrown.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:5
- Comments:10 (5 by maintainers)
Top Results From Across the Web
️ Using React hooks for functional components with perfect ...
Rewrite all components to functional components - Big NO NO. Duplicate the same logic in both hooks and hoc format. But what do...
Read more >React component is not exported correctly (can't use hooks ...
So I'm trying to build a generic component in a separate repo from a project. The components repo is an npm package.
Read more >Export Your Logic into React Hooks | JavaScript in Plain English
With hooks, it's possible to split the logic by creating custom hooks managing all your business logic and only returning UI formatted data....
Read more >Importing and Exporting Components - React Docs
Export your function component from that file (using either default or named exports). Import it in the file where you'll use the component...
Read more >Avoid ES6 default exports - Rajesh Naroth - Medium
More from Rajesh Naroth · Replace Redux with React Hooks · TypeScript: The good parts — 1. React Prop-types · TypeScript: Add to...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Hey @artalar when using aliasing of
react
topreact/compat
you should usereact
imports rather thanpreact
. In other words, you should useimport { useContext } from "react"
.Check out the amended version https://codesandbox.io/s/hello-world-1xs58
@developit @marvinhagemeister
10.0.0-rc.3
- same bug.(reproduce, see
Header.tsx
)I try to
import { useContext } from "preact/hooks";
andimport { useContext } from "preact/compat";
, but every time get the same error:TypeError: Cannot read property 'context' of undefined
.By the way, all works fine if I firstly start the server with code without hooks, and after that try to add code with hooks (hot reload -> all works fine). May be the problem related to SSR (next.js)?