React Answers

How to fix “Cannot read property ‘pathname’ of undefined” error with React Router?

Spread the love

To fix "Cannot read property ‘pathname’ of undefined" error with React Router, we need to add the Routes properly.

For instance, we write

import React from 'react';
import ReactDOM from 'react-dom';
import {
  BrowserRouter as Router,
} from 'react-router-dom';

const Main = () => <h1>Hello world</h1>;

    <Route path='/' component={Main} />

if we’re using React Router v4.

We add the Router and put our Routes inside it.

Leave a Reply

Your email address will not be published.

If you like the content of this blog, subscribe to my email list to get exclusive articles not available to anyone else.