【问题标题】:Context compatible React hook for routing用于路由的上下文兼容的 React 钩子
【发布时间】:2020-02-16 15:07:21
【问题描述】:

我在我的所有 react 项目中都使用了钩子和功能组件,并且没有任何类组件。所以我想要一个用于路由的钩子(所以不想使用 react-router 库)。我用谷歌搜索并找到了一些钩子并选择了hookrouter。由于应用程序组件在路由更改时重新渲染,因此不能使用 Context 将一些数据与项目一起保存。我想要一个与 react-context 兼容的路由钩子(当然是 context-provider 和 useContext)。有人知道吗?感谢您抽出宝贵时间。

【问题讨论】:

  • 这可能会帮助你:) medium.com/@Charles_Stover/…
  • @BhojendraRauniyar 非常感谢,但我之前研究过。问题是实现起来并不像我想要的那么简单

标签: reactjs routes react-hooks react-context hookrouter


【解决方案1】:

使用 useHistory 挂钩进行导航。该钩子与路由器dom版本5.1.0及以上版本兼容。

import { useHistory } from 'react-router-dom'

const history = useHistory();

history.push({pathname:'/home'});

【讨论】:

  • 非常感谢,但现在我正在使用useHistory。在我写这个问题的时候,这个钩子不存在;)
猜你喜欢
  • 1970-01-01
  • 2020-08-18
  • 2020-09-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-09
  • 2017-05-05
  • 2021-07-09
相关资源
最近更新 更多