Cannot read property 'history' of undefined (useHistory hook of React Router 5)

前端 未结 6 1443
离开以前
离开以前 2020-12-17 08:14

I am using the new useHistory hook of React Router, which came out a few weeks ago. My React-router version is 5.1.2. My React is at version 16.10.1. You can find my code at

6条回答
  •  北海茫月
    2020-12-17 08:41

    Use BrowserRouter.

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

    If you use Router, then you need to specify a history for it:

    import {
      Router,
      Route,
      Switch,
    } from 'react-router-dom';
    import createBrowserHistory from 'history';
    
    const history = createBrowserHistory();
    return (
      
        ...
      
    );
    

提交回复
热议问题