Skip to content Skip to sidebar Skip to footer

Uncaught Typeerror: Cannot Read Property 'push' Of Undefined With Correct Import Being Available

I am using react-router and have seen the other questions asked already. May be I am blind or this may be a valid issue. index.js has const Root = () => (

Solution 1:

You're using react-router v4 and it doesn't support anymore using browserHistory like this. You can access history from your route component's (SpicyMenuItem) props and you can call push method of it like this.

props.history.push('/menuDetail/' + menuId);

Also you don't need provid browserHistory when you define <Router>. Use new <BrowserRouter> instead.

Solution 2:

My Component is not a Router Component, so I had to wrap it with withRouter API. The solution looks like

import {withRouter} from'react-router';
classSpicyMenuextendsReact.Component {

    constructor(props) {
        super(props);

        this.state = {
            foodItems: foodItems
        }
    }

    render() {
        return (
            <List>
                {this.state.foodItems.map(foodItem => <SpicyMenuItemWithRouterkey={foodItem.id} {...foodItem}/>)}
            </List>

        );
    }
}
constSpicyMenuItemWithRouter = withRouter(SpicyMenuItem);

That fixed the issue. Thanks everyone for the help

Post a Comment for "Uncaught Typeerror: Cannot Read Property 'push' Of Undefined With Correct Import Being Available"