【发布时间】:2018-12-08 19:50:28
【问题描述】:
假设我有这个父母
const Parent = () => <ChildComponent foo={<Button>clic</Button>} />
为什么这段代码可以工作
class ChildComponent extends React.Component {
render() {
return (
<div>
{this.props.foo}
</div>
)
}
}
但这段代码不会?
class ChildComponent extends React.Component {
constructor(props) {
super(props)
const ParentButton = this.props.foo
}
render() {
return (
<div>
<ParentButton />
</div>
)
}
}
我需要类似于第二个示例的内容,以便向 ParentButton 添加一些事件。
我想让this example 使用类定义的组件
更新:
根据答案,我现在有了部分解决方案
class ChildComponent extends React.Component {
constructor(props) {
super(props)
this.ParentButton = this.props.foo
}
render() {
return (
<div>
{this.ParentButton}
</div>
)
}
}
【问题讨论】: