useHistory Cannot read property 'push' of undefined

788 阅读1分钟

The issue is because your Router component is nested inside of your App component rather than your App being nested inside of your router. The router acts as sort of a context provider and without your app being nested in the router the useHistory() hook has no provider to get the History from.

简而言之,使用useHistory必须在Router组件中使用。