[英]How to pass a component as props to another component in react js?
我有一个这样的组件
<ReactSuperSelect placeholder="Nothing Selected"
clearSearchOnSelection={true}
deselectOnSelectedOptionClick={true}
dataSource={testData}
multiple={true}
onChange={this.handlerExample}
keepOpenOnSelection={true}
searchable={true} />
我将其分配给变量并作为props传递,但它像字符串一样显示在其中。 如何实现呢?
您需要如下声明组件
const myComponent = <ReactSuperSelect placeholder="Nothing Selected"
clearSearchOnSelection={true}
deselectOnSelectedOptionClick={true}
dataSource={testData}
multiple={true}
onChange={this.handlerExample}
keepOpenOnSelection={true}
searchable={true} />
接下来,将它作为道具传递给childComponent,如下所示。
<ChildComponent subComponent={myComponent}/>
假设prop的变量名是subComponent
(如上所示),您可以在ChildComponent
的render方法中显示通过props传递的组件,如下所示。
render() {
return (
<div>
{this.props.subComponent}
</div>
)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.