我尝试将类的引用动态传递给容器并呈现它。
class Test extends React.Component{
render() {
return <div>Test</div>
}
}
class HelloWidget extends React.Component{
constructor(props) {
super(props);
this.state = {
child: Test
};
}
render() {
return <div>{this.state.child}</div>;
}
}
React.render(<HelloWidget />, document.getElementById('container'));
在 jsfiddle 看到它:https ://jsfiddle.net/coolshare/jwm6k66c/2720/
它没有渲染任何东西......
有什么建议吗?
谢谢
您需要使用React.createElement
来创建新的 react 元素。
像这样:
render() {
return <div>hello {React.createElement(this.state.child)}</div>;
}
检查工作小提琴。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句