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.

Issue using Hooks with exported component

See original GitHub issue

Bug

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

  1. Clone https://github.com/oliverdunk/preact-bug
  2. Run npm install
  3. 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:closed
  • Created 4 years ago
  • Reactions:5
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
cristianbotecommented, Oct 1, 2019

Hey @artalar when using aliasing of react to preact/compat you should use react imports rather than preact. In other words, you should use import { useContext } from "react".

Check out the amended version https://codesandbox.io/s/hello-world-1xs58

1reaction
artalarcommented, Oct 1, 2019

@developit @marvinhagemeister 10.0.0-rc.3 - same bug.

(reproduce, see Header.tsx)

I try to import { useContext } from "preact/hooks"; and import { 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)?

Read more comments on GitHub >

github_iconTop 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 >

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