close

[Solved] Uncaught TypeError: Cannot read property ‘push’ of undefined (React-Router-Dom)

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error Uncaught TypeError: Cannot read property ‘push’ of undefined (React-Router-Dom) in javascript. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How Uncaught TypeError: Cannot read property ‘push’ of undefined (React-Router-Dom) Error Occurs?

Today I get the following error Uncaught TypeError: Cannot read property ‘push’ of undefined (React-Router-Dom) in javascript.

How To Solve Uncaught TypeError: Cannot read property ‘push’ of undefined (React-Router-Dom) Error ?

  1. How To Solve Uncaught TypeError: Cannot read property 'push' of undefined (React-Router-Dom) Error ?

    To Solve Uncaught TypeError: Cannot read property 'push' of undefined (React-Router-Dom) Error My mistake was the wrong import in conjuction with BrowserRouter, ie:
    incorrect:
    import { useHistory } from 'react-router'
    correct:
    import { useHistory } from 'react-router-dom'

  2. Uncaught TypeError: Cannot read property 'push' of undefined (React-Router-Dom)

    To Solve Uncaught TypeError: Cannot read property 'push' of undefined (React-Router-Dom) Error My mistake was the wrong import in conjuction with BrowserRouter, ie:
    incorrect:
    import { useHistory } from 'react-router'
    correct:
    import { useHistory } from 'react-router-dom'

Solution 1

for React-router V4 change the function to

onClick={this.fun.bind(this)}

fun() {
  this.props.history.push("/Home");
}

and

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

export it later as:

export default withRouter (comp_name);

Solution 2

My mistake was the wrong import in conjuction with BrowserRouter, ie:

incorrect:

import { useHistory } from 'react-router'

correct:

import { useHistory } from 'react-router-dom'

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also Read

Leave a Comment