深圳龙岗是穷人区吗,搜索引擎排名优化方案,软件系统开发平台,免费网站可以做cpa一、简介 react router是一个构建基于react应用的路由管理库。允许你在程序中定义不同的路由和导航规则。以实现不同的url路径显示不同的组件。
二、相关技术
Routerdivul id menuliLink to /homeHomeRouterdivul id menuliLink to /homeHome/Link/liliLink to /helloHello/Link/liliLink to /aboutAbout/Link/li/uldiv id page-containerRoute path /home component {Home} /Route path /hello component {Hello} /Route path /about component {About} //div/div
/Router
Link实现导航跳转
Route 配置路由定义对应组件会填充父组件。
React Router API
Link - Link to /aboutAbout/Link 普通连接不会触发浏览器刷新
NavLink - NavLink to /about activieClassName selectedAbout/NavLink
Prompt - Prompt when {fromIsHalfFilledOut} message Are you sure you want to leave/
Redirect - Route exact path / render {() (loggedIn ? (Redirect to /dashboard/):(PublicHomePage/))} /
url传参
react 组件可以通过 match props 获取Route 中url对应的占位符值。
// 通过match属性获取Route Path中的占位符值
const Topic ({match}) (h1Topic {match.params.id}/h1
); export default class RouterParams extends React.PureComponent{render(){return (Routerdivul id menuliLink to /topic/1Topic 1/Link/liliLink to /topic/2Topic 2/Link/liliLink to /topic/3Topic 3/Link/li/uldiv id page-containerRoute path /topic/:id component {Topic} //div/div/Router);}}
默认情况下直接修改浏览器地址是不会触发跳转的必须通过Link或者其它React routeApi实现跳转。
嵌套路由
1.每个React组件都是路由容器。
2.React Router的声明式语法可以方便的定义嵌套路由。
举个多级目录嵌套路由例子
// 一级目录
export const Category () {return () {Routerdivul id menuliLink to /category/1Category 1/Link/liliLink to /category/2Category 2/Link/liliLink to /category/3Category 3/Link/li/uldiv id nav-containerRoute path /category/:id component {SubCategory}/div/div/Router}
}// 二级目录
export const SecondCategory ({match}) {return () {Routerdivul id menuliLink to /category/${match.params.id}/sub/1Category 1/Link/liliLink to /category/${match.params.id}/sub/2Category 2/Link/liliLink to /category/${match.params.id}/sub/3Category 3/Link/li/uldiv id page-containerRoute path /category/:id/sub/:subId component {Page}/div/div /Router}
}// 页面内容
export const Page ({match}) {const data getPageData(match.params.id, match.params.subid);return parseData(data);
}