Home > Web Front-end > JS Tutorial > body text

Share an example of obtaining routing parameters in react components

零下一度
Release: 2017-06-17 10:32:44
Original
1729 people have browsed it

This article mainly introduces the detailed explanation of how react obtains routing parameters in components. It has certain reference value. Interested friends can refer to

routing. Parameters

Suppose we have many list pages. Except for the dynamic content of these pages, other page parts are the same. How do we configure routing and components at this time?

In this scenario, you need to use the routing parameter function and add a routing configuration containing parameters.


import List from './component/list';

<Route path="list/:id" component={List} />
Copy after login

Note that :id in the path attribute is the parameter (param) of the route. Let's take a look at the components of the List page.


/list 对应了 list.js

import React from &#39;react&#39;;
class List extends React.Component {
 render () {
 return (
 <p>
 <h3>This is List page.</h3>
 <p>The list page id is 
  <b style={{color: &#39;red&#39;}}>{this.props.params.id}</b>
 </p>
 </p>
 );
 }
};
export default List;
Copy after login

In the List component, you can access the actual parameter values ​​directly through this.props.params.id (the id key here is similar to the :id that defines the path Corresponding), React Router passes all routing data to page components through props, so that you can access routing-related data very conveniently.

The above is the detailed content of Share an example of obtaining routing parameters in react components. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template