admin管理员组文章数量:1287052
I am declaring a component inside of a parent component. I want to establish specific props in one file, and then in the parent component, I want to be able to establish the other props for the child components all at once (since they are shared properties.. for the most part).
My problem is that the child component attempts to render and fails since the required prop types aren't being established at first.
Is there a way to tell the child component to wait for the required props to render?
In the ParentComponent render function, I call another function to run React.Children.map() and append props to ChildComponent.
The (rough) section where the child component is being rendered:
<ParentComponent>
<ChildComponent
attribute="test_attribute"
label="Child Component"
type="number"
width={3}
/>
</ParentComponent>
I am declaring a component inside of a parent component. I want to establish specific props in one file, and then in the parent component, I want to be able to establish the other props for the child components all at once (since they are shared properties.. for the most part).
My problem is that the child component attempts to render and fails since the required prop types aren't being established at first.
Is there a way to tell the child component to wait for the required props to render?
In the ParentComponent render function, I call another function to run React.Children.map() and append props to ChildComponent.
The (rough) section where the child component is being rendered:
<ParentComponent>
<ChildComponent
attribute="test_attribute"
label="Child Component"
type="number"
width={3}
/>
</ParentComponent>
Share
Improve this question
asked May 18, 2016 at 19:39
cmwallcmwall
4672 gold badges6 silver badges15 bronze badges
1
- 1 Possible duplicate of How can i block a React component to be rendered until I fetched all informations? – azium Commented May 18, 2016 at 19:45
1 Answer
Reset to default 24You can use a conditional render statement to only render the child when the props are populated:
let {foo, bar} = this.props;
<ParentComponent>
{ foo && bar ? <ChildComponent foo={foo} bar={bar} /> : null }
</ParentComponent>
Or instead of null
you could render a <LoadingSpinner />
or something.
本文标签: javascriptReact Component wait for required props to renderStack Overflow
版权声明:本文标题:javascript - React Component wait for required props to render - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738463525a2088167.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论