两者都有Route、Link等,什么时候使用其中之一?我真的很困惑在哪里使用每一个。服务器端?客户端?
https://reacttraining.com/react-router/
在某些示例中您需要传递历史记录,而在其他示例中则不需要。该怎么办?
<Router history={browserHistory}>
对
<Router>
何时使用其中一种确实令人困惑,任何帮助表示赞赏。
react-router包含react-router-dom和react-router-native之间的所有公共组件。什么时候应该使用其中一种而不是另一种?如果您在网络上,那么 react-router-dom 应该拥有您需要的一切,因为它还导出您需要的通用组件。如果您使用 React Native,出于同样的原因,react-router-native 应该拥有您所需的一切。所以你可能永远不需要直接从 react-router 导入任何东西。就当你使用
<Router history={browserHistory}>
对
<Router>
在 RRv4 中,您不需要传递 browserHistory,这仅适用于以前版本的路由器。
如果您仍然感到困惑,您可以查看每个套餐的详细信息这里
react-router-dom 是一个
react-router
加:
<BrowserRouter>
这是
<Router history={browserNativeHistoryApiWrapper}/>
浏览器的一些链接改进
证明:https://github.com/ReactTraining/react-router/blob/master/packages/react-router-dom/modules/Link.js
和
<NavLink>
— 知道它是否“活动”的包装器
只需使用
react-router-dom
- react-router-dom
即可重新导出所有 react-router
。 GitHub 上的链接答案(react-router-dom
和 react-router
之间有什么区别?)。
在 v4 中,react-router 导出了核心组件和功能。 React-router-dom 导出 DOM 感知组件,例如
( 渲染<Link>
) 和 (与浏览器的交互 窗口.历史记录)。<a>
重新导出所有react-router的导出,所以你 只需要从项目中的react-router-dom
导入即可。react-router-dom
在 Github 中找到了这个。
注意:这个包提供了 React Router 的核心路由功能,但你可能不想直接安装它。如果您正在编写将在浏览器中运行的应用程序,则应该安装
react-router-dom
。同样,如果您正在编写 React Native 应用程序,则应该安装 react-router-native
。这两个都将安装 react-router
作为依赖项。
来源:https://github.com/remix-run/react-router/tree/main/packages/react-router#readme