码迷,mamicode.com
首页 > 其他好文 > 详细

[React] React Router: Named Components

时间:2016-03-29 14:46:05      阅读:189      评论:0      收藏:0      [点我收藏+]

标签:

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;

 

[React] React Router: Named Components

标签:

原文地址:http://www.cnblogs.com/Answer1215/p/5332607.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!