36

I am trying to programmatically change pages using browserHistory.push. In one of my components, but not in a component that I embedded inside of that one.

Does anyone know why my project is throwing the error below only for the child component but not for the parent component?

Cannot read property 'push' of undefined

Parent Component

import React, {Component} from 'react';
import { browserHistory } from 'react-router';
import ChildView from './ChildView';

class ParentView extends Component {

constructor(props) {
    super(props);
    this.addEvent = this.addEvent.bind(this);
  }

changePage() {
    this.props.history.push("/someNewPage");
  }

render() {
    return (
      <div>
        <div>
          <button onClick={this.changePage}>Go to a New Page!</button>
        </div>

        <ChildView />  // this is the child component where this.props.history.push doesn't work 

      </div>
    );
  }
}

function mapStateToProps(state) {
    return {
        user: state.user
    };
}

function matchDispatchToProps(dispatch) {
    return bindActionCreators({
      setName: setName
    }, dispatch)
}

export default connect(mapStateToProps, matchDispatchToProps)(ParentView);

Child Component

    import React, {Component} from 'react';
    import { browserHistory } from 'react-router';

    class ChildView extends Component {

    constructor(props) {
        super(props);
        this.addEvent = this.addEvent.bind(this);
      }

    changePage() {
        this.props.history.push("/someNewPage");
      }

    render() {
        return (
          <div>
            <div>
              <button onClick={this.changePage}>Go to a New Page!</button>
            </div>

          </div>
        );
      }
    }

    function mapStateToProps(state) {
        return {
            user: state.user
        };
    }

    function matchDispatchToProps(dispatch) {
        return bindActionCreators({
          setName: setName
        }, dispatch)
    }

    export default connect(mapStateToProps, matchDispatchToProps)(ChildView);

Router

// Libraries
import React from 'react';
import { BrowserRouter as Router, Route, Switch } from 'react-router-dom';
import { browserHistory } from 'react-router';

// Components
import NotFound from './components/NotFound';
import ParentView from './components/ParentView';
import ChildView from './components/ChildView';
import SomeNewPage from './components/SomeNewPage';

// Redux
import { Provider } from 'react-redux';
import {createStore} from 'redux';
import allReducers from './reducers';

const store = createStore(
  allReducers,
  window.devToolsExtension && window.devToolsExtension()
);

const routes = (
    <Router history={browserHistory}>
      <div>
        <Provider store={store}>
            <Switch>
              <Route path="/" exact component={Home} />
              <Route path="/parentView" component={ParentView} />
              <Route path="/someNewPage" component={SomeNewPage} />
              <Route path="/childView" component={ChildView} />
              <Route component={NotFound} />
            </Switch>
        </Provider>
      </div>
    </Router>
);

export default routes;

As you can see, the components are virtually exactly the same except that the child one is inside the parent one.

Note I have tried these approaches but they do not resolve the issue for me:

Community
  • 1
  • 1
Rbar
  • 3,740
  • 9
  • 39
  • 69

5 Answers5

47

You answered your question in your question.

As you can see, the components are virtually exactly the same except that the child one is inside the parent one.

The very fact that the component is nested one further is your issue. React router only injects the routing props to the component it routed to, but not to the components nested with in.

See the accepted answer to this question. The basic idea is that you now need to find a way to inject the routing props to the child component. You can do that by wrapping the child in a HOC withRouter.

export default withRouter(connect(mapStateToProps, matchDispatchToProps)(ChildView));

I hope this helps.

Community
  • 1
  • 1
Chaim Friedman
  • 6,124
  • 4
  • 33
  • 61
  • Since I still want to continue connecting my component to redux as well, do you know how you would you combine my `export default connect(mapStateToProps, matchDispatchToProps)(ChildView);` line and the recommended `export default withRouter(Child)` line from your link? – Rbar May 08 '17 at 00:32
  • 1
    Just to answer the question for others, `export default withRouter(connect(mapStateToProps, matchDispatchToProps)(ChildView));` works like a charm. Huge thanks, @promisified! – Rbar May 08 '17 at 00:47
  • What about defining a function in routed component and passing as props to its child. Now calling this function as this.props we can get the expected behaviour. – learner Jan 04 '18 at 07:04
34

Using withRouter is fine, an alternative option is to pass the history as a prop from parent to child (without using withRouter), e.g:

Parent

<SignupForm history={this.props.history}/>

Child

this.props.history.push('/dashboard');
Notorious
  • 3,057
  • 3
  • 23
  • 33
  • 1
    Is this definitely a safe/good practice? This was the solution I discovered for my app before finding this question. My child component didn't need full routing props, just the ability to push to history, and this seems like a pretty clean solution to me. (Sorry I'm super new to React!) – Brian Apr 30 '18 at 15:51
  • 1
    Good solution, the problem is that when we are in nested components, we need to pass props of history from the particular parent that is connected to routing. – Danish Feb 19 '19 at 20:01
  • Very Useful answer. – Aboobakkar P S Apr 30 '19 at 09:24
4

You Can try this

this.context.history.push('/dashboard')

or pass the history as a prop from parent to child component like

parent

<SignupForm history={this.props.history}/>

child

this.props.history.push('/dashboard');

or withRouter

import { withRouter } from "react-router";
export default withRouter(connect(mapStateToProps, {
    ...
})(Header));
Zahid Hassan Shaikot
  • 1,066
  • 10
  • 18
3

This is the problem of Nested Components. I was facing this issue in Header component. I wanted to redirect the page to home page on clicking the logo.

this.props.history.push won't work in nested components.

So Solution is to use withRouter .

To Use withRouter just copy paste Below 2 lines in Header(You can use it in your component) :

import { withRouter } from "react-router";
export default withRouter(connect(mapStateToProps, {
    ...
})(Header));
0

If anyone is still facing similar issues please try to change the onClick to this

onClick={() => this.props.history.push("/dashboard")}

also if you are in a child component please use the <Redirect to="/dashboard"/> component

jerryurenaa
  • 3,863
  • 1
  • 27
  • 17