Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React-Router v4: Cannot read property 'route' of undefined

Tags:

react-router

I want to redirect when I hit a button, so I used the withRouter to get the access to the history props.

But I get the error:

Uncaught TypeError: Cannot read property 'route' of undefined
  at Route.computeMatch (react-router.js:1160)

error when I wrap my component with the withRouter HOC. If I remove withRouter function, it just works.

My code looks like the following:

class App extends Component {

// ...some unrelated functions

handleTitleTouchTap = e => {
    e.preventDefault()
    const { history } = this.props
    history.push('/')
}

render() {
                //...other components
        <Router>
            <div>      
                <Switch>
                    <Route exact={true} path="/" component={Home} />
                    <Route path="/search" component={Search}/>
                    <Route path="/gamelist/:listId" component={GameListDetail}/>
                    <Route path="/game/:gameId" component={GameDetail}/>
                    <Route path="/manageuser" component={ManageUser} />
                    <Route path="/addgamelist" component={AddGameList} />
                    <Route path="/addgame" component={AddGame} />
                    <Route path="/test" component={Test} />
                    <Route component={NoMatch} />
                </Switch>
                <LoginForm isLoginFormOpen={isLoginFormOpen} closeLoginForm={closeLoginForm} handleLogin={handleLogin}/>
                <RegisterForm isRegisterFormOpen={isRegisterFormOpen} closeRegisterForm={closeRegisterForm} register={register}/>
            </div>
        </Router>
    )
}


const mapStateToProps = state => ({
    //some props
})
const mapDispatchToProps = dispatch => ({
    //some functions
})
const Container = connect(mapStateToProps, mapDispatchToProps)(App)

export default withRouter(Container)
like image 667
leuction Avatar asked Apr 03 '17 03:04

leuction


People also ask

Can not read the property location of undefined?

The error "Cannot read property 'pathname' of undefined" occurs when we don't set the to prop on a Link component in React router. To solve the error, set the to prop on the Link to the specific path, e.g. <Link to="/">Home</Link> . Here is a minimal example of using the Link component in React router. Copied!

How do I listen to route changes in react v6 router?

Use your router and pass your history object to it. In a component you want to listen to location changes on, import your history object and invoke the listen callback as you did previously. import history from '../myHistory'; ... useEffect(() => { const unlisten = history.

How do you handle not found route in react?

Use a wildcard placeholder to handle 404 page not found in React router, e.g. <Route path="*" element={<PageNotFound />} /> . A route that has an asterisk path * serves as a catch all route. It only matches when no other routes do.

How do I enable routing in react?

To enable routing in our React app, we first need to import BrowserRouter from react-router-dom . This should hold everything in our app where routing is needed. That means, if we need routing in our entire app, we must wrap our higher component with BrowserRouter .


1 Answers

I've got the same issue and I solved it enclosing the wrapped component in a Router component (namely BrowserRouter).

In your example, it would become:

// assuming this file is Container.js
export default withRouter(Container)


// index.js
import Container from './Container'

render(
    <BrowserRouter>
        <Container/>
    </BrowserRouter>,
    document.getElementById('root')
 )

Working example from the docs here: https://codepen.io/pietro909/pen/RVWmwZ

I also opened an issue on the repo because the example from the docs is not clear enough in my opinion https://github.com/ReactTraining/react-router/issues/4994.

like image 141
pietro909 Avatar answered Oct 11 '22 08:10

pietro909