Javascript Reactjs-`component`vs`render`在路由中
我对react-router-domv4.3.1中路由的使用有两个疑问: 何时在管线中使用组件vs渲染: 如何以两种方式访问URL中的变量username?Javascript Reactjs-`component`vs`render`在路由中,javascript,reactjs,routes,react-router-dom,Javascript,Reactjs,Routes,React Router Dom,我对react-router-domv4.3.1中路由的使用有两个疑问: 何时在管线中使用组件vs渲染: 如何以两种方式访问URL中的变量username? 将组件传递给组件属性时,该组件将获得props.match.params对象中的路径参数,即示例中的props.match.params.username: class ProfileComponent extends React.Component { render() { return <div>{this.pr
将组件传递给组件属性时,该组件将获得props.match.params对象中的路径参数,即示例中的props.match.params.username:
class ProfileComponent extends React.Component {
render() {
return <div>{this.props.match.params.username}</div>;
}
}
当您需要来自包含路由的组件的一些数据时,通常会使用渲染道具,因为组件道具没有向组件传递其他道具的实际方式。最后一句话是所有其他解释都忽略的关键部分。非常感谢。
class ProfileComponent extends React.Component {
render() {
return <div>{this.props.match.params.username}</div>;
}
}
<Route
exact
path='/u/:username/'
render={(props) =>
<ProfileComponent username={props.match.params.username}/>
}
/>