博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
react router 4.0以上的路由应用
阅读量:4635 次
发布时间:2019-06-09

本文共 1452 字,大约阅读时间需要 4 分钟。

 

react router 4.0以上的路由应用

在4.0以下的react router中,嵌套的路由可以放在一个router标签中,形式如下,嵌套的路由也直接放在一起。

但是在4.0以后,嵌套的路由与之前的就完全不同了,需要单独放置在嵌套的根component中去处理路由,否则会一直有warning:

You should not use <Route component> and <Route children> in the same route

正确形式如下

//

上面将嵌套的路由注释掉

const Users = ({ match }) => (  

Topics

)

上面在需要嵌套路由的component中添加新的路由

一个完整的嵌套路由的例子如下

说明及注意事项

  1. 以下代码采用ES6格式
  2. react-router-dom版本为4.1.1
  3. 请注意使用诸如HashRouter之类的history,否则一直会有warning,不能渲染
import React, { Component } from 'react';import ReactDOM from 'react-dom';// import { Router, Route, Link, Switch } from 'react-router';import {  HashRouter,  Route,  Link,  Switch} from 'react-router-dom';class App extends Component {  render() {    return (      

App

  • Home
  • About
  • Inbox
{this.props.children}
); }}const About = () => (

About

)const Home = () => (

Home

)const Message = ({ match }) => (

new messages

{match.params.id}

)const Inbox = ({ match }) => (

Topics

) ReactDOM.render( (
), document.getElementById('root'));

参考

转载于:https://www.cnblogs.com/sylarmeng/p/6920903.html

你可能感兴趣的文章
【Netty】ChannelHandler和ChannelPipeline
查看>>
Spring Boot2.0+中,自定义配置类扩展springMVC的功能
查看>>
windows下部署免费ssl证书(letsencrypt)
查看>>
字符串处理示例--列车车次查询.sql
查看>>
Erlang 位串和二进制数据
查看>>
图片上传
查看>>
H5学习之旅-H5列表(8)
查看>>
华为机试题【10】-求数字基root
查看>>
ISLR—第二章 Statistical Learning
查看>>
软件与程序
查看>>
tiny4412u-boot烧写及根文件系统制作(不进入终端问题)
查看>>
谁说菜鸟不会数据分析--读书笔记
查看>>
nodejs里的module.exports和exports
查看>>
搭建wordpress开发环境
查看>>
CentOS 6.9下的Setup工具(用于管理服务/防火墙/网络配置/验证服务)
查看>>
Mac 10.12彻底关闭Dashboard
查看>>
mybatis大于小于等于
查看>>
mac下安装前端模板引擎Jinja2
查看>>
python 字典
查看>>
How to: Build a Client Application
查看>>