我开始涉足ReactJS,我试图向标签传递一个JSON对象,这样我就可以在UI中显示它,但它一直在说找不到该元素.有什么帮助吗?还是为什么props对象没有JSON对象?提前致谢
var CommentBox = React.createClass({
render: function() {
return (
<div className="img-container">
<img src="{this.props.placementImage}" />
</div>
);
}
});
var MP = [
{
id: "MP1001",
placementImage: "https://www.aexp-static.com/intl/uk/rwd/images/UKHP_CM_promo_3.png",
dts: "forever",
dte: "forever",
status: "",
isDefault: false
}
];
ReactDOM.render(
<CommentBox mp={MP}/>,
document.getElementById('content')
);
解决方法:
几件事:
>您正在传递一个数组作为mp prop,但随后尝试像对象一样访问它.
>您需要从< img>中删除引号src属性:
>您需要访问实际的mp prop
作为参考,我从您的代码中创建了一个JSBin示例,该示例解决了以下问题:http://jsbin.com/bohoqa/edit?html,js,output
var CommentBox = React.createClass({
render: function() {
return (
<div className="img-container">
<img src={this.props.mp.placementImage} />
</div>
);
}
});
var MP = [
{
id: "MP1001",
placementImage: "https://www.aexp-static.com/intl/uk/rwd/images/UKHP_CM_promo_3.png",
dts: "forever",
dte: "forever",
status: "",
isDefault: false
}
];
ReactDOM.render(
<CommentBox mp={MP[0]}/>,
document.getElementById('content')
);