[英]Reactjs - `component` vs `render` in Route
I have two doubts regarding usage of Route
from react-router-dom
( v4.3.1 ): 关于使用来自
react-router-dom
( v4.3.1 )的Route
,我有两个疑问:
When do we use component
vs render
in Route
: 我们什么时候在
Route
使用component
vs render
:
<Route exact path='/u/:username/' component={ProfileComponent} /> <Route exact path='/u/:username/' render={() => <ProfileComponent />} />
username
in the URL in both ways? username
? When you pass a component to the component
prop, the component will get the path parameters in the props.match.params
object, ie props.match.params.username
in your example: 将组件传递给
component
prop时,组件将获取props.match.params
对象中的路径参数,即props.match.params.username
中的props.match.params.username
:
class ProfileComponent extends React.Component {
render() {
return <div>{this.props.match.params.username}</div>;
}
}
When using the render
prop, the path parameters can be accessed through the props given to the render
function: 使用
render
道具时,可以通过赋予render
功能的道具访问路径参数:
<Route
exact
path='/u/:username/'
render={(props) =>
<ProfileComponent username={props.match.params.username}/>
}
/>
You generally use the render
prop when you need some data from the component that contains your routes, since the component
prop gives no real way of passing in additional props to the component. 当您需要包含路径的组件中的某些数据时,通常使用
render
道具,因为component
道具没有提供将额外道具传递给component
真实方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.