【发布时间】:2018-03-20 00:01:35
【问题描述】:
正如标题所说,我需要访问所有孩子的地图功能的每个child元素,React.Children.map(this.props.children, (child)...
我需要这个,因为我想有条件地渲染某些道具,并且还根据当前正在渲染的孩子阻止基于某些条件的渲染。
我已经在构造函数中绑定了这个函数
this.renderChildren = this.renderChildren.bind(this);
但它仍然无法正常工作。
我什至可以让这个 map 函数工作的唯一方法是将它包装在 return() 函数中。有什么想法吗?
renderChildren(funcs) {
// debugger
return (
React.Children.map(this.props.children, (child) => {
debugger // *** Need to access `this.state` from in here ***
return React.cloneElement(child, {
state: this.state, // *** Need access here too
callbackFuncs: funcs
})
})
)
}
...
return(<div>{this.renderChildren(callbacks)}</div>)
以下内容将不起作用(未包含在退货中)
renderChildren(funcs) {
React.Children.map(this.props.children, (child) => {
return React.cloneElement(child, {
state: this.state,
callbackFuncs: funcs
})
})
}
【问题讨论】:
标签: reactjs this state children