[React] React Router: Named Components

Posted Answer1215

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[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;

 

以上是关于[React] React Router: Named Components的主要内容,如果未能解决你的问题,请参考以下文章

react缓存页面react-keepalive-router

浅析react中的react-router和react-router-dom

react之react-router v6

React:react-router-dom 详解

[React] React Router: Router, Route, and Link

React Router(react-router-dom V6 整理)