设置useHistory挂钩时,无法读取未定义的属性“ history”

时间:2020-08-04 08:20:12

标签: reactjs electron react-hooks

我遇到的错误是Cannot read property 'history' of undefined。我已经从useHistory()正确导入了react-router-dom钩子。

export const App = () => {
  
  /* Handle deep link navigation based on incoming myApp://entity/:entity_id call */
  const history = useHistory()

  const setLocationFromDeepLink = (path: string) => {
    history.push(`/${path}`)
  }

.......

1 个答案:

答案 0 :(得分:1)

请确保您使用的是来自react-router-dom的BrowserRouter 因为useHistory()仅适用于BrowserRouter。

import { BrowserRouter} from 'react-router-dom'
<BrowserRouter>
        ...
         Do logic here
        ...
 </BrowserRouter>

您可以从下面的链接中了解有关react-router-dom的更多信息

https://reactrouter.com/web/api/Hooks