React 快速上手 - 07 前端路由 react-router
目录
React 快速上手 - 07 前端路由 react-router目标
环境
0. 安装
通过官网我们可以发现 我们这里针对 yarn add react-router-dom
1. 先跑一个简单例子
import React,{Component} from 'react' import {HashRouter as Router,Route,Link,Switch} from 'react-router-dom' const Home = () => ( <div> <h2>首页</h2> </div> ) const About = () => ( <div> <h2>关于</h2> </div> ) class RouterView extends Component { render() { return ( <Router> <div> <ul> <li> <Link to="/">首页</Link> </li> <li> <Link to="/about">关于</Link> </li> </ul> <Switch> <Route exact path="/" component={Home} /> <Route path="/about" component={About} /> </Switch> </div> </Router> ) } } export default RouterView
最外层需要包裹组件
https://codepen.io/ducafecat/... 2. 基础使用2.1
|
path | 组件 | 说明 |
---|---|---|
/article | ArticleList | 文章列表 |
/article/:id | Article | 文章 |
/article/:id/recommend | ArticleRecommend | 文章推荐 |
6. 自定义路由
适合用来做权限检查
6.1 创建自定义 Route
const isAuthenticated = true const PrivateRoute = ({ component: Component,...rest }) => ( <Route {...rest} render={props => isAuthenticated ? ( <Component {...props} /> ) : ( <Redirect to={{ pathname: "/login",state: { from: props.location } }} /> ) } /> );
6.2 使用自定义路由
... <Route path="/public" component={Public} /> <Route path="/login" component={Login} /> <PrivateRoute path="/protected" component={Protected} />
7. 转换动画
这里用到了 react-transition-group
库
动图效果
7.1 安装
yarn add react-transition-group
7.2 编写动画 css
文件 fade.css
.fade-enter { opacity: 0; z-index: 1; } .fade-enter-active { opacity: 1; transition: opacity 250ms ease-in; } .fade-exit { opacity: 0; }
名称 | 说明 |
---|---|
fade-enter | 动画启动 |
fade-enter-active | 动画激活 |
fade-exit | 动画离开 |
其它动画样式名字可以参考 css-transition
7.3 导入包和动画样式
... import {TransitionGroup,CSSTransition} from 'react-transition-group' import './fade.css'
TransitionGroup,CSSTransition
必须导入
7.4 编写样式
const styles = {} styles.fill = { position: 'relative',height: '200px',width: '500px' } styles.content = { ...styles.fill,top: '40px',textAlign: 'center',height: '120px' } styles.nav = { padding: 0,margin: 0,position: 'absolute',top: 0,height: '40px',width: '100%',display: 'flex' } styles.navItem = { textAlign: 'center',flex: 1,listStyleType: 'none',padding: '10px' } styles.hsl = { color: 'white',paddingTop: '20px',fontSize: '30px',height: '120px' } styles.rgb = { color: 'white',height: '120px' }
这是 react
的样式对象,当然你也可以写成 .css
文件
7.5 编写导航
const NavLink = props => ( <li style={styles.navItem}> <Link {...props} style={{color: 'inherit'}} /> </li> )
7.6 编写展示组件
// 切换区域 A const HSL = ({match: {params}}) => ( <div style={{ ...styles.fill,...styles.hsl,background: `hsl(${params.h},${params.s}%,${params.l}%)` }} > hsl({params.h},{params.s}%,{params.l}%) </div> ) // 切换区域 B const RGB = ({match: {params}}) => ( <div style={{ ...styles.fill,...styles.rgb,background: `rgb(${params.r},${params.g},${params.b})` }} > rgb({params.r},{params.g},{params.b}) </div> )
7.7 编写容器组件
const RouterView = () => ( <Router> <Route render={({location}) => ( <div style={styles.fill}> <Route exact path="/" render={() => <Redirect to="/hsl/10/90/50" />} /> <ul style={styles.nav}> <NavLink to="/hsl/10/90/50">Red</NavLink> <NavLink to="/hsl/120/100/40">Green</NavLink> <NavLink to="/rgb/33/150/243">Blue</NavLink> <NavLink to="/rgb/240/98/146">Pink</NavLink> </ul> <div style={styles.content}> <TransitionGroup> <CSSTransition key={location.key} classNames="fade" timeout={300}> <Switch location={location}> <Route exact path="/hsl/:h/:s/:l" component={HSL} /> <Route exact path="/rgb/:r/:g/:b" component={RGB} /> <Route render={() => <div>Not Found</div>} /> </Switch> </CSSTransition> </TransitionGroup> </div> </div> )} /> </Router> )
- codepen
https://codepen.io/ducafecat/...
代码
- reactjs-example / 5-1-routerBase
- reactjs-example / 5-2-routerParams
- reactjs-example / 5-3-routerNoMatch
- reactjs-example / 5-4-routerTransitions
- reactjs-example / 5-5-routerRedirect
参考
- react-router
- react-transition-group
- code-splitting-in-create-react-app
(编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!