react-router4@笔记


新项目使用react-router@4来进行路由跳转,使用非static的配置方式该用jsx。之前配置动态生成路由一直报错,发现提示route必须要有父级,在外面嵌套了div,才成功了,但是在官方文档中并未提及,也没看懂他的用意。


0、 页面入口

import React,{Component} from "react";
import reactDom from 'react-dom';
import "../css/index.less";
import SNCRouter from "./route"

class Index extends Component{
    componentWillReceiveProps(){

    }
   componentDidMount(){
      console.log("index")
   }
   render(){
       return <section>
           <SNCRouter />
       </section>
   }
}
reactDom.render(<Index />,document.querySelector("#SNC-router"))

1、路由代码

import React, { Component } from "react";
import { HashRouter as Router, Route, Link, Switch } from 'react-router-dom';
import { send, store } from "../controller/reduxList.js";
import { routerIndex } from '../route/route-index';
const routers = [...routerIndex];
class SNCRouter extends Component {
    set404() {
        return <div>404</div>
    }
    setRoute() {
        return routers.map((r, i) => {
            return <Route key={i} {...r} />
        })
    }
    render() {
        return <Router>
            <Switch>
                {
                    this.setRoute()
                }
                <Route component={this.set404} />
            </Switch>
        </Router>

    }
}

export default SNCRouter;
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 本文转自我的博客阅读原文。 重要API一览 路由容器组件 BrowserRouter: 浏览器自带的API,res...
    xuan241阅读 5,480评论 0 1
  • Lesson11、首先确保安装了Node.js和npm依赖包管理工具2、在git上克隆官方示例程序 git clo...
    冰_心阅读 7,616评论 0 16
  • 深入JSX date:20170412笔记原文其实JSX是React.createElement(componen...
    gaoer1938阅读 12,480评论 2 35
  • 今天的蓝天,朵朵白云掩盖。抬头仰望,竟有一丝怔忪。一丝抽痛将我从放空中拉回。脖子很疼,工作一天了,有点累,也...
    曼曼冰冰阅读 1,432评论 1 1
  • 快速培养你的逻辑思维能力 干事没逻辑,再累也是白忙活!很多人做事没有逻辑,东一下,西一下,说话不知所云,自己累,听...
    拆书家孟钢阅读 3,573评论 0 2

友情链接更多精彩内容