我已经尝试过,但没有成功。
var osDD = React.createClass({
propTypes: {
dat: React.PropTypes.array
},
render: function(){
var li = this.props.dat;
console.log(li);
return (
<ul><li></li></ul>
)
}
});
var osPre = React.createClass({
render: function(){
var osdd = React.createElement(
osDD,
{dat: this.props.dat}
);
return (
<div>
<header>{this.props.title}</header>
<p>{this.props.description}</p>
<span className="opt_serv_price">{this.props.price}</span>
<osdd/>
</div>
)
}
});
这只是呈现一个空标签 <osdd/>
在 DOM 中。
也许我没有按照 React 的方式来做。我最近在看了一些视频和阅读之后才开始使用 React。
最佳答案
好吧,我偶然发现了这个网站 ( http://ricostacruz.com/cheatsheets/react.html#nesting ),它讨论了 React 中的嵌套。
因此尝试相应地遵循并将我的代码更新为:
var osDD = React.createClass(/*no changes*/);
var osPre = React.createClass({
render: function(){
return (
<div>
<header>{this.props.title}</header>
<p>{this.props.description}</p>
<span className="opt_serv_price">{this.props.price}</span>
<osDD/>
</div>
)
}
});
但是还是没有成功。通过查看官方文档( http://facebook.github.io/react/docs/multiple-components.html )中的更多示例,我注意到它们都使用大写的变量定义了 ReactClass。
所以我必须将我的类名从 osDD
更改为 OsDD
并且它起作用了!我想我错过了文档中关于此的某个地方。如果有人能指出我所说的位置,我将不胜感激。
关于javascript - 我可以在 React 类中包含 React 类吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34193204/