有没有办法将一个组件传递到另一个React组件?我想创建一个模型react组件,并传入另一个react组件以包含该内容。
编辑:这是一个reactJS codepen,说明了我正在尝试做的事情。http://codepen.io/aallbrig/pen/bEhjo
的HTML
<div id="my-component">
<p>Hi!</p>
</div>
ReactJS
/**@jsx React.DOM*/
var BasicTransclusion = React.createClass({
render: function() {
// Below 'Added title' should be the child content of <p>Hi!</p>
return (
<div>
<p> Added title </p>
{this.props.children}
</div>
)
}
});
React.renderComponent(BasicTransclusion(), document.getElementById('my-component'));
Actually, your question is how to write a Higher Order Component (HOC). The main goal of using HOC is preventing copy-pasting. You can write your HOC as a purely functional component or as a class here is an example:
If you want to write your parent component as a class-based component:
If you want to write your parent as a functional component: