在你父亲组件<Father />
,你可以(使用和id为实例),分配一个布尔值,这意味着呈现不创造一个可以跟踪每一个孩子的初始状态该组件安装,您启动定时器来改变状态:
componentDidMount() {
this.timeouts = React.Children.forEach(this.props.children, (child, index) => {
return setTimeout(() => {
this.setState({ index: true; });
}, child.props.delay);
});
}
当你渲染你的孩子,你通过重新创建他们这样做,分配为道具的匹配子说,如果组件必须的状态被渲染或不被渲染。
let children = React.Children.map(this.props.children, (child, index) => {
return React.cloneElement(child, {doRender: this.state[index]});
});
所以在你<Child />
组件
render() {
if (!this.props.render) return null;
// Render method here
}
当超时被触发,状态改变和父亲组件重新呈现。儿童道具更新了,如果doRender
是true
,他们会渲染自己。
非常感谢您的回答!完全按照我的需要工作。 – michalvalasek
你从哪里得到那个show property?那不是React的一部分吗? – PositiveGuy
'show'指的是我创建的函数,它将'hidden'状态设置为空字符串。这是'Child'类的第三个功能。 –