profile
viewpoint

Ask questionsCannot read property 'location' of undefined at useLocation

<!-- Thank you for contributing!

Have a usage question?

The issue tracker is meant for bug reports only. This isn't the best place for usage questions. Questions here don't have as much visibility as they do elsewhere. Before you ask a question, here are some resources to get help first:

  • Read the docs: https://reacttraining.com/react-router
  • Check out the list of frequently asked questions: https://github.com/ReactTraining/react-router/blob/master/FAQ.md
  • Explore examples: https://reacttraining.com/react-router/web/example/basic
  • Look for/ask questions on Stack Overflow: https://stackoverflow.com/questions/ask?tags=react-router
  • Ask in chat: https://discord.gg/0ZcbPKXt5bYaNQ46

Think you found a bug?

The best bug report is a failing test in the repository as a pull request. Otherwise, please use the "BUG REPORT" template below.

Have a feature request?

Remove the template from below and provide thoughtful commentary and code samples on what this feature means for your product. What will it allow you to do that you can't do today? How will it make current work-arounds straightforward? What potential bugs and edge cases does it help to avoid? etc. Please keep it product-centric. -->

<!-- BUG TEMPLATE -->

Version

"react": "^16.11.0", "react-router-dom": "^5.1.2"

Test Case

https://stackblitz.com/edit/react-router-uselocation-error

Steps to reproduce

1 - Create a variable referencing a useLocation call. 2 - Try to access the variable.

Expected Behavior

The variable being accessible.

Actual Behavior

An error happens in line 713 of the react-router.js file of the react-router package.

Uncaught TypeError: Cannot read property 'location' of undefined
    at useLocation (react-router.js?4f2e:713)

The fragment is:

...
function useLocation() {
  if (process.env.NODE_ENV !== "production") {
    !(typeof useContext === "function") ? process.env.NODE_ENV !== "production" ? invariant(false, "You must use React >= 16.8 in order to use useLocation()") : invariant(false) : void 0;
  }

  return useContext(context).location; // Error here.
}
...

Am I missing something?

Thanks.

ReactTraining/react-router

Answer questions lewislbr

Ahhh got it! Thanks!

useful!

Related questions

Route is not exported in react-router-dom hot 6
Error: Invariant failed: You should not use <Switch> outside a <Router> hot 3
Replace componentWillMount by UNSAFE_componentWillMount for v3 hot 3
Use new forwardRef API in withRouter HOC hot 2
useParams returns empty object when used outside of Route hot 2
Failed prop type: Invalid prop 'component' supplied to 'Route': the prop is not a valid React component hot 2
useParams and other new hooks are not exported in react-router-dom & react-router hot 2
react-router-dom v5 does not work with electron in production hot 2
Prompt in React 16 "Warning: A history supports only one prompt at a time" hot 1
Fix <Route children> hot 1
Route is not exported in react-router-dom hot 1
bug: react router dom 4.3.1 pulling react-router 4.4.0 changes after latest publish hot 1
[React.memo] Warning: Failed prop type: Invalid prop `component` of type `object` supplied to `Route`, expected `function` hot 1
Receiving ESM Error but not using ESM hot 1
useParams and other new hooks are not exported in react-router-dom & react-router hot 1
source:https://uonfu.com/
Github User Rank List