我在父组件内部声明一个组件。我想在一个文件中建立特定的道具,然后在父组件中,我希望能够一次为子组件全部建立其他道具(因为它们大部分是共享属性。)。
我的问题是子组件尝试呈现并失败,因为最初没有建立所需的道具类型。
有没有办法告诉子组件等待所需的道具渲染?
在ParentComponent渲染函数中,我调用另一个函数来运行React.Children.map()并将道具附加到ChildComponent上。
呈现子组件的(粗糙)部分:
<ParentComponent> <ChildComponent attribute="test_attribute" label="Child Component" type="number" width={3} /> </ParentComponent>
您可以使用条件渲染语句仅在填充道具时渲染孩子:
let {foo, bar} = this.props; <ParentComponent> { foo && bar ? <ChildComponent foo={foo} bar={bar} /> : null } </ParentComponent>
或者可以代替null渲染<LoadingSpinner />。
null
<LoadingSpinner />