0

I have a code where i need to show home page after clicking the button.When i call the same route '/Sub' in other page,it is working. But here it throws Cannot read property 'push' of undefined

Routes.js :

import React from 'react';
import { Switch, BrowserRouter, Route } from 'react-router-dom';

const Routes = () => (
    <BrowserRouter>
        <Switch>
            <Route exact path='/' component={Addpage} />
            <Route path='/New' component={Details} />
            <Route path='/Sub' component={Displaydata} />
        </Switch>
    </BrowserRouter>
) 
export default Routes;

Here is the UI code :

class Displaypage extends Component {
    constructor(props) {
        super(props);
        this.state = {
            Pageid: 1
        }
        this.handleSubmit = this.handleSubmit.bind(this);
        this.handleAddNew = this.handleAddNew.bind(this);
    }

    handleSubmit = s => {
        s.preventDefault();
        const Pageid = this.state.Pageid;
        this.props.history.push({
            pathname: "/Sub",
            state: {
                Pageid: this.state.Pageid;
            }
        });
    }

    render() {
        const submitbutton = <button onClick={this.handleSubmit}>Submit</button>
        return (
            <div >
                {submitbutton}
            </div>
        );
    }
}
export default Displaypage;

Kishan Bharda
  • 5,446
  • 3
  • 30
  • 57
shiju
  • 11
  • 8

2 Answers2

0

Have you defined your compoenent with https://reacttraining.com/react-router/web/api/withRouter ?

import { withRouter } from "react-router"

class Displaypage extends Component {
    constructor(props) {
        super(props);
        this.state = {
            Pageid:1
}            
        this.handleSubmit=this.handleSubmit.bind(this);
        this.handleAddNew = this.handleAddNew.bind(this);
    }

handleSubmit = s => {
        s.preventDefault();
        const Pageid = this.state.Pageid;
        this.props.history.push({
            pathname: "/Sub",
            state: {
                Pageid: this.state.Pageid;
            }
        });
    }
     render() {

const submitbutton = <button onClick={this.handleSubmit}>Submit</button>

return (
            <div >                              
                {submitbutton}              

            </div>
        );

}
}   

const DisplaypageWithRouter = withRouter(Displaypage)

export default DisplaypageWithRouter;
Horatiu Jeflea
  • 7,256
  • 6
  • 38
  • 67
0

I'm not sure if its a typo, but the component name is Displaypage whereas the routed component is to Displaydata?

Apart from that, I don't see anything wrong and should work as is. As other answers suggest, withRouter isn't needed for a direct component under Router.

**Route props All three render methods will be passed the same three route props

match location history **

Karthik R
  • 5,523
  • 2
  • 18
  • 30
  • Yes its a typo. It works only after including withRouter – shiju Oct 08 '19 at 15:38
  • I understand its works, but an immediate component being rendered under a router, by default has match, history and location props. Unless the component name is not a typo. – Karthik R Oct 09 '19 at 00:15