如何在使用传播属性时获取子组件中的道具



Containers是子组件。我使用 separate 在父组件中将 props 传递给它。

const props = {key:_("containerList"),
containers: this.state.containers,
containersStats: this.state.containersStats,
onlyShowRunning: this.state.onlyShowRunning,
updateContainers: this.updateContainers,
updateContainerStats: this.updateContainerStats
}
containerList=
<Containers
{...props}
></Containers>  

现在我想在子组件Containers中获取道具,该怎么做?this.props.containers?还是this.props.updateContainers

既然你使用的是spread operator.它枚举所有 props 并将这些 props 传递给子组件。所以在你的<Containers />组件中,你可以像这样访问你的道具:

const { key,
containers,
containersStats,
onlyShowRunning,
updateContainers,
updateContainerStats }= this.props

如果您需要任何进一步的澄清,请告诉我。

最新更新