简体   繁体   English

将所有状态和道具传递给孩子

[英]passing to children all state and props in react

I have the following code on my render: 我的渲染器上有以下代码:

<TourHeader key="TourHeader" {...this.props, ...this.state} />

I get the following error: 我收到以下错误:

Syntax error: Unexpected token, expected }

Only this was working: 只有这是有效的:

<TourHeader key="TourHeader" {...this.props} />

How do I solve this? 我该如何解决?

您可以分别传播它们:

<TourHeader key="TourHeader" {...this.props} {...this.state} />

You need to split them up into separate attribute spreads, eg: 您需要将它们分成单独的属性传播,例如:

<TourHeader key="TourHeader" {...this.props} {...this.state} />

Hope this helps! 希望这可以帮助!

I suggest you first look up what the spread operator does in javascript. 我建议您首先查看价差运算符在javascript中的功能。 Then you will realize the mistake you did. 然后,您将意识到自己犯的错误。 What you want is passing props and state to the child component. 您想要的是将道具和状态传递给子组件。 Do that separately like this. 像这样单独做。

<TourHeader key="TourHeader" {...this.props} {...this.state} />

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

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