11

i am quite new to react and react-router.

Problem with react is here that, on some pages of my application react-router is working and some giving error like this: "Cannot read property 'push' of undefined".

i am using react 0.14.1

My routing code looks like this:

render(
<Router history={hashHistory}>
  <Route path="/" component={Loginpanel}/>
  <Route path="Index" component={Index}/>
  <Route path="Form" component={Form} />
  <Route path="Checkindex" component={Index}/>

  <Route path="Signup" component={Signup}/>
  <Route path="Admin" component={Admin}/>
  <Route path="AdminEditDetails" component={AdminEditDetails}/>
  <Route path="AdminDetails" component={AdminDetails}/>


</Router>,
document.getElementById('js-main'));

My component is like this now:

class  App extends React.Component {

  constructor(props) {
    super(props);
    this.state= {      
      comments: AppStore.getAll();
    };
  }

  componentWillMount() {  
    var length = this.state.comments.length;
    var firstnumber = length - 4;

    if(length == 0){
      console.log("here comes");
      this.props.router.push('/');
    }
    else{
      console.log('work normally');
    }
  }

}

Can anyone help me with this? Thanks.

Nader Dabit
  • 52,483
  • 13
  • 107
  • 91

7 Answers7

15

Using hashHistory directly from react-router is certainly an option, but it makes unit-testing a bit more painful. Browser history will generally not be available in the context tests run in and it's easier to mock out a prop than to mock out a global API.

Consider using the withRouter higher-order-component that react-router provide (since v2.4.0).

import { withRouter } from 'react-router';

class App extends Component {

    (...)

}

export default withRouter(App);

Your App class will receiver router via props and you can safely do this.props.router.push('/');.

ivarni
  • 17,658
  • 17
  • 76
  • 92
  • Is this a better approach than the accepted answer? – Doug Aug 30 '17 at 08:35
  • 1
    @Doug I think so, personally, which is why I posted it but YMMV :) – ivarni Aug 30 '17 at 08:45
  • Any link explaining usage of `hashHistory`. Can you please also give some link explaining first paragraph, mainly mock part. Quite new to react and web development in general. – Rnj Jun 09 '21 at 21:20
10

If you are use Partial Component and calling it like <Header /> then you should replace it by <Header {...this.props} />

Jitendra Suthar
  • 2,111
  • 2
  • 16
  • 22
4

Your app does not have an instance of Router in its props which is giving you Cannot read property 'push' of undefined.

I'm assuming you're importing withRouter to get the instance of the Router so you'd need to wrap the component in that if you still want to use that... (example here but not suggested)

Instead, a better approach to programmatically navigating is to use

import { hashHistory } from 'react-router;' ... hashHistory.push('/'); in your componentWillMount lifecycle event.

The docs are here

Hope this helps!

Patrick Pei
  • 437
  • 4
  • 9
1

You are using hashHistory. The following code should work.

import { hashHistory } from 'react-router';
hashHistory.push('/');

The root route should also be defined.

<Route path="/" component={App}>
vijayst
  • 20,359
  • 18
  • 69
  • 113
1

you can try the below code see if your router props is working or not

componentWillMount() {

var length = this.state.comments.length;
var firstnumber = length - 4;
if(length == 0){
    console.log("here comes");
  if(this.props.router !== undefined) {
    this.props.router.push('/');
  } else {
    console.log(this.props.router);
  }
}
else{
    console.log('work normally');
}
}

also you can visit their documentation to get more information from below url https://github.com/reactjs/react-router/blob/master/docs/guides/NavigatingOutsideOfComponents.md

Md.Estiak Ahmmed
  • 1,553
  • 9
  • 14
0

You can get access to the history prop from witRouter higher order component, and use the push method on the history object to push the route you want to.

import { withRouter } from "react-router";

class App extends React.Component {

    componentWillMount() {
        ...
        this.props.history.push('/');
        ...
    }

}

export default withRouter(App);
Prateek Surana
  • 672
  • 9
  • 29
  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 17 '21 at 09:05
-3

Add two lines.

import { withRouter } from "react-router-dom";
this.props.history.push("/welcomePage");
vkstream
  • 881
  • 8
  • 8