所以我正在观看一个教程,其中讲师在 route 使用 {...props}。如果他已经传递了 user 和 getUser 属性,为什么还要使用它?:
<Route exact path="/user/:login" render={props => (
<User {...props} getUser={this.getUser} user={user} />
)}
/>
顺便说一句,在 User.js 中,这些 props 被传递到其中,他使用: this.props.getUser(this.props.match.params.login)。会不会和这个有关系?
最佳答案
Why would he use it if he is alreading passing user and getUser props?
这样任何其他 Prop 都会像foo
和bar
一样传递。
In the User.js, where those props are being passed to, he uses: this.props.getUser(this.props.match.params.login). Is it possible it has something to do with this?
没有。正如你所说,他是明确提供该 Prop ,而不是通过传播。通过在传播之后这样做,他确保使用他的显式版本(无论您是否将 getUser
传递给容器的 props,它都不会被使用,因为他的 getUser
覆盖了它;与 user
相同)。
关于javascript - 为什么要在 React Route 中使用展开运算符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59614643/