0

While using react-router-dom 5.3.0, I am opening a modal but am not changing route, and this error appears.

I have since downgraded react-router-dom to 5.2.0 and this error does not appear.

Any help is much appreciated :-)

Error Message

5
  • context.location is not defined Commented Oct 4, 2021 at 10:50
  • Hi @BENARDPatrick, could you expand a bit more, I'm not sure what to do with this in terms of my code. Commented Oct 4, 2021 at 15:35
  • You see the second stack trace error ? line 104 createPath(context.location) ? It calls the function createPath and inside it, the line 47 location is undefined. So context.location is undefined.... In your code, you can wait that context.location has a wanted value... Commented Oct 4, 2021 at 16:10
  • Thanks @BENARDPatrick. All of these errors take place within a package (which I believe is react-router-dom) in my node_modules. So I was thinking that the problem lies in the package? Commented Oct 5, 2021 at 10:51
  • Anyway, I just upgraded react-router-dom again to the latest version and I can't actually replicate the problem, lets see if the problem has now gone. Commented Oct 5, 2021 at 10:52

1 Answer 1

1

I had the same error after upgrading the react-router-dom version when I tried accessing the current URL with location.pathname .

Using the useLocation() hook solved my problem:

import { useLocation } from 'react-router-dom';

// getting current pathname with custom hook

const useName = () => {
const location = useLocation();
return location.pathname;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.