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.

[useContext] Throw error if 'useContext' is used outside function components

See original GitHub issue

Do you want to request a feature or report a bug? Feature (need better errors)

What is the current behavior? Consider the following functional component

import React, { useContext } from "React"

const myFunctionComponent = props => <div>Hello useContext</div>

The immediate reaction for most of us (newbies to hooks) to refactor the above code to accomodate useContext is as follows

import React, { useContext } from "React"
import MyContext from "./MyContext"

// React does not throw error
const { myContextValue } = useContext(MyContext)

const myFunctionComponent = props => <div>Hello useContext - {myContextValue}</div>

The way to actually refactor is to explictly convert the arrow function return expression into a function body and then accomodate useContext inside along with a return statement, like this

import React, { useContext } from "React"
import MyContext from "./MyContext"

const myFunctionComponent = props => {
 const { myContextValue } = useContext(MyContext)
 return (<div>Hello useContext - {myContextValue}</div>)
}

Not only, react does not throw error, React app actually compiles, while the component in question fails to load with no information. This is very difficult to pin the reason to this specific issue.

What is the expected behavior?

React should ideally throw some kind of error, when useContext is used outside of function components. This lack of error really bites us for people who are refactoring function components without a return statement.

Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?

React 16.8.x with hooks support

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
palerdotcommented, Mar 21, 2019

@gaearon Gotcha … The problem is with react-loadable which for some reason swallows the error thrown by React. A reproducible snippet here - https://codesandbox.io/embed/l9m2v4l2zz. I’m also using react-loadable in my local project and after lots of trial and error, pinned down this react-loadable issue. You can see the ‘loading’ text without any errors, which should actually show the hooks error. The invalid hooks call is in hello.js file

Let me know if this is enough info.

0reactions
palerdotcommented, Mar 21, 2019

I think this is not an issue with react per se and can be closed.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use React.useContext in a function that does not ...
1 Answer 1 · It didn't work. I think it's because a hook isn't a hook if it's not returning JSX to a...
Read more >
How to use useContext in functional components?
A complete guide on How to use useContext in functional components including a step by step guide with examples.
Read more >
Why I never use React.useContext - Julian​Garamendy​.dev
Conclusion. We can make errors appear earlier (unfortunately still at runtime) when we render a component outside the required Context Provider ...
Read more >
Validate use of Context using a custom hook and ... - Egghead.io
If it's null, we issue an error indicating that the compound component cannot be rendered outside of the barium with our component. Otherwise, ......
Read more >
How to use React Context like a pro - Devtrium
import React, { useContext, createContext, useState, ... undefined) { throw new Error("useUserContext was used outside of its Provider"); } ...
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