有没有办法将一个组件传递给另一个react组件?我想创建一个模型react组件,并传入另一个react组件,以便转换该内容。
编辑:这是一段reactJS代码,说明了我想要做的事情。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'));
发布于 2014-09-12 19:37:02
您可以使用this.props.children
呈现组件包含的任何子项:
const Wrap = ({ children }) => <div>{children}</div>
export default () => <Wrap><h1>Hello word</h1></Wrap>
发布于 2017-03-17 08:59:01
const ParentComponent = (props) => {
return(
{props.childComponent}
//...additional JSX...
)
}
//import component
import MyComponent from //...where ever
//place in var
const myComponent = <MyComponent />
//pass as prop
<ParentComponent childComponent={myComponent} />
发布于 2018-11-15 00:28:31
你可以将它作为一个普通的道具来传递:foo={<ComponentOne />}
例如:
const ComponentOne = () => <div>Hello world!</div>
const ComponentTwo = () => (
<div>
<div>Hola el mundo!</div>
<ComponentThree foo={<ComponentOne />} />
</div>
)
const ComponentThree = ({ foo }) => <div>{foo}</div>
https://stackoverflow.com/questions/25797048
复制相似问题