zoukankan      html  css  js  c++  java
  • [React] React Router: Named Components

    In this lesson we'll learn how to render multiple component children from a single route.

    Define a named component by "components":

    <Route path="/other" components={ {header: Other, body: OtherBody}}></Route>

    'header' and 'body' are the key.

    Render:

    const Container = (props) => <div>{props.header}{props.body}<Links /></div>;

    ------------------

    import React from 'react';
    import {hashHistory, Route, Router, Link, IndexRoute} from 'react-router';
    
    const Home = () => <h1>Home</h1>;
    const HomeBody = () => <h3>HomeBody</h3>;
    const Other = () => <h1>Other</h1>;
    const OtherBody = () => <h3>OtherBody</h3>;
    
    const Container = (props) => <div>{props.header}{props.body}<Links /></div>;
    
    const Links = () =>
        <nav >
            <Link to="/">Home</Link>
            <Link to="/other">Other</Link>
        </nav>;
    
    class App extends React.Component {
        render(){
            return(
                <Router history={hashHistory}>
                    <Route path="/" component={Container}>
                        <IndexRoute components={ {header: Home, body: HomeBody} }></IndexRoute>
                        <Route path="/other" components={ {header: Other, body: OtherBody}}></Route>
                    </Route>
                </Router>
            );
        }
    }
    
    export default App;
  • 相关阅读:
    Java 课程设计:LWZ
    回溯法解骑士巡游问题
    2021.3.30 错误2
    2021.3.29 关于上下滚动
    2021.3.28 WebView的用法
    2021.3.27 关于错误1
    2021.3.26 Python创建表
    2021.3.25 人月神话阅读笔记06
    2021.3.24 个人作业第三阶段1
    2021.3.23 个人作业第三阶段
  • 原文地址:https://www.cnblogs.com/Answer1215/p/5332607.html
Copyright © 2011-2022 走看看