简体   繁体   English

routerProps 是什么意思?

[英]What does routerProps mean?

有人可以解释一下 routerProps 和 {...routerProps} 是什么意思吗?

<Route path='/movies' render={routerProps => <MoviesPage {...routerProps} movies={this.state.movies}/>} />

Your question lacks context, for example, what routing library and version are you using?您的问题缺乏上下文,例如,您使用的是什么路由库和版本? It could also easily be answered with a quick documentation reading or google search.也可以通过快速阅读文档或谷歌搜索轻松回答。 Ill assume the library is react-router.我假设图书馆是反应路由器。 routeProps (not routerProps) is an object holding, unsurprisingly, the route props. routeProps(不是 routerProps)是一个持有路由道具的对象,不出所料。 check https://reactrouter.com/core/api/Route/route-props检查https://reactrouter.com/core/api/Route/route-props

Additionally {...routeProps} creates a new object passing the routeProps properties as properties for the new object.另外 {...routeProps} 创建一个新对象,将 routeProps 属性作为新对象的属性传递。 its called spread operator and in this case its applied to an object literal.它称为扩展运算符,在这种情况下,它应用于对象文字。 See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax请参阅https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax

This pattern calledRender Props .这种模式称为Render Props

A component with a render prop takes a function that returns a React element and calls it instead of implementing its own render logic.带有 render prop 的组件接受一个返回 React 元素的函数并调用它,而不是实现自己的渲染逻辑。

In your example, Route 's prop render accepts a function with variable routerProps which of type object (because you can spread its entries {...object} using spread syntax ) and renders a React element.在您的示例中, Route的 prop render接受一个带有变量routerProps的函数,它的类型为 object (因为您可以使用spread syntax传播其条目{...object} )并呈现 React 元素。

<Route render={routerProps => <MoviesPage {...routerProps} movies={this.state.movies}/>} />

// Same, props === { path: 'some path' }
<Route render={props => <MoviesPage path={props.path} movies={this.state.movies}/>} />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM