- react-router是跨平台的,内置通用组件和通用Hooks
- react-router-dom是在react-router基础上提供了Link和NavLink
- 依赖的 history库提供了两个浏览器端适用的BrowserRouter和HashRouter
- 安装了
react-router-dom
,npm
会解析并安装上述依赖包。可以看到,其中包括react-router
react-router-dom npm包
"dependencies": {
"history": "^4.7.2",
"invariant": "^2.2.2",
"loose-envify": "^1.3.1",
"prop-types": "^15.5.4",
"react-router": "^4.2.0",
"warning": "^3.0.0"
}
hooks方法
useHistory()
useLocation()
useParams()
useRouteMatch()
react-router-dom方法
import {
BrowserRouter as Router,
// HashRouter as Router,
MemoryRouter,
StaticRouter,
Router,
Route,
Switch,
Link,
NavLink,
Prompt,
Redirect,
history,
location,
generatePath,
match,
withRouter,
} from 'react-router-dom'
react-router和 react-router-dom的区别
- react-router 4.x以及之前的版本,叫 react-router
- react-router 5之后,将原本的针对网页使用的react-router的npm package命名改为了react-router-dom
- 一般都用的react-router-dom代替 react-router,一些常用的组件都封装好了
- 最新的react项目直接使用react-router-dom,不用react-router