-
-
Notifications
You must be signed in to change notification settings - Fork 47
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
react-router和react-router-dom的区别 #42
Labels
Comments
受教了 |
分析得很清楚学习了 |
我也正尝试作用 |
清清楚楚、明明白白,喜欢这样的文字。 |
thx |
感谢作者 |
thx |
写得很清楚,谢谢 |
thx |
受教了 |
thx |
2 similar comments
thx |
thx |
little sun |
分析明白 |
清楚明了 |
阿里戈多 |
十分感谢,简洁明了 |
我一个java开发表示看懂了 |
受教了 |
么么哒 |
3q |
谢了,解决了困惑 |
3q |
统计来访者的插件数据很有趣,US第一...想半天才反应过来是VPN,妙啊 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
react-router
v4,我称之为“第四代react-router”,react-router
和react-router-dom
的区别是什么呢?为什么有时候我们看到如下的写法:
写法1:
写法2:
先简单说下各自的功能:
react-router
: 实现了路由的核心功能react-router-dom
: 基于react-router
,加入了在浏览器运行环境下的一些功能,例如:Link
组件,会渲染一个a
标签,Link组件源码a
标签行;BrowserRouter
和HashRouter
组件,前者使用pushState
和popState
事件构建路由,后者使用window.location.hash
和hashchange
事件构建路由。react-router-native
: 基于react-router
,类似react-router-dom
,加入了react-native
运行环境下的一些功能。从源码层面来说明:
首先看
react-router-dom
中的Switch
组件的源码只是从
react-router
中导入Switch
组件,然后重新导出而已。查看其他模块的源码,
Route
组件的源码Router
组件的源码...
和
Switch
一样,都是从react-router
中导入了相应的组件,重新导出而已,并没有对实现做什么特殊处理。结论:
react-router-dom
依赖react-router
,所以我们使用npm
安装依赖的时候,只需要安装相应环境下的库即可,不用再显式安装react-router
。基于浏览器环境的开发,只需要安装react-router-dom
;基于react-native
环境的开发,只需要安装react-router-native
。npm
会自动解析react-router-dom
包中package.json
的依赖并安装。react-router-dom
中package.json
依赖:安装了
react-router-dom
,npm
会解析并安装上述依赖包。可以看到,其中包括react-router
。参考链接:
remix-run/react-router#4648
https://github.com/ReactTraining/react-router/blob/master/packages/react-router/README.md
The text was updated successfully, but these errors were encountered: