React Router的Route的使用

Route 是 React Router中用于配置路由信息的组件,每当有一个组件需要根据 URL 决定是否渲染时,就需要创建一个 Route。

1) path

每个 Route 都需要定义一个 path 属性,path 属性是一个url,当 URL 匹配一个 Route 时,这个 Route 中定义的组件就会被渲染出来。

2)Route 渲染组件的方式

(1)component

component 的值是一个组件,当 URL 和 Route 匹配时,Component属性定义的组件就会被渲染。例如:

<Route path=‘/foo‘ component={Foo} >

当 URL = "http://example.com/foo" 时,Foo组件会被渲染。

(2) render

render 的值是一个函数,这个函数返回一个 React 元素。这种方式方便地为待渲染的组件传递额外的属性。例如:

<Route path=‘/foo‘ render={(props) => {
 <Foo {...props} data={extraProps} />
}}>
</Route>

Foo 组件接收了一个额外的 data 属性。

(3)children

children 的值也是一个函数,函数返回要渲染的 React 元素。 与前两种方式不同之处是,无论是否匹配成功, children 返回的组件都会被渲染。但是,当匹配不成功时,match 属性为 null。例如:

<Route path=‘/foo‘ render={(props) => {
 <div className={props.match ? ‘active‘: ‘‘}>
  <Foo {...props} data={extraProps} />
 </div>
}}>
</Route> 

如果 Route 匹配当前 URL,待渲染元素的根节点 div 的 class 将设置成 active.

原文地址:https://www.cnblogs.com/SharkBin/p/10164787.html

时间: 2024-10-08 22:19:27

React Router的Route的使用的相关文章

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

In this lesson we'll take our first look at the most common components available to us in react-router; Router, Route, and Link. import React from 'react'; import {hashHistory, Route, Router, Link} from 'react-router'; const Home = () => <div><

react router 4.0以上的路由应用

thead>tr>th{padding:8px;line-height:1.4285714;border-top:1px solid #ddd}.table>thead>tr>td,.table>tbody>tr>th,.table>tbody>tr>td,.table>tfoot>tr>th,.table>tfoot>tr>td{padding:8px;line-height:1.4285714;ver

[React] React Router: Nested Routes

Since react-router routes are components, creating nested routes is as simple as making one route a child of another in JSX. Make the nested component: class App extends React.Component { render(){ return( <Router history={hashHistory}> <Route pa

[React] React Router: IndexRoute

IndexRoute allows us to define a default child component to be rendered at a specific route when no other sub-route is available. When Home page display, we also make About component as default Route to dsiplay, only when use click Contact link, then

[React Router v4] Use the React Router v4 Link Component for Navigation Between Routes

If you’ve created several Routes within your application, you will also want to be able to navigate between them. React Router supplies a Link component that you will use to make this happen. Import Link: import { BrowserRouter as Router, Route, Link

[React Router V4] Create Basic Routes with the React Router v4 BrowserRouter

React Router 4 has several routers built in for different purposes. The primary one you will use for building web applications is the BrowserRouter. In this lesson you will import the BrowserRouter and create some basic Route components. After create

[Redux] Navigating with React Router &lt;Link&gt;

We will learn how to change the address bar using a component from React Router. In Root.js: We need to add a param to change the Route: import React from 'react'; import {Provider} from 'react-redux'; import {Router, Route, browserHistory } from 're

[Redux] Adding React Router to the Project

We will learn how to add React Router to a Redux project and make it render our root component. Install: npm install --save react-router import React from 'react'; import {Provider} from 'react-redux'; import {Router, Route} from 'react-router'; impo

[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 '