我是 React 新手,遇到了以下使用 React Router 的代码:
<Router>
<NavLink to="/homepage">Homepage</NavLink>
<Route path="/homepage" render={props =>
(<Homepage {...props} pieceOfState={this.state.pieceOfState}/>)
}/>
</Router>
问题是传递到渲染“render={props =>...”的 props 是否代表路由器 props,就像对象匹配、位置和历史记录一样?
最佳答案
...props
确实代表匹配、位置和历史。当您有额外的 props 要传递时使用此语法。就像在本例中一样,this.state.pieceOfState
来自docs :
The render prop function has access to all the same route props (match, location and history) as the component render prop.
您还可以通过以下方式单独传递所需的 Prop :
<Route path="/homepage" render={({ match }) =>
(<Homepage match={match} pieceOfState={this.state.pieceOfState}/>)
关于javascript - 与 React R 中传递给渲染的 props 混淆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58006796/