【发布时间】:2014-09-28 03:34:47
【问题描述】:
我想从myComponent 外部调用setProps,以便能够动态更改myComponent 的数据。
我希望在更改组件的道具后,它会重新渲染自己。
我正在尝试以下方法:
var myComponent = React.createClass({
render: function () {
return (
React.DOM.div(null, this.props.data)
);
}
});
React.renderComponent(
myComponent({ data: someData }),
document.getElementById('predictionContent')
);
myComponent.setProps({data: someData2});
问题是我不明白如何为组件使用setProps。
就我而言,我收到“未定义”错误。
如何解决?
【问题讨论】:
-
旁注:myComponent 是组件类。 setProps 唯一起作用的地方是组件实例,您只能在组件的一个方法中使用
this或从渲染它的组件中使用this.refs。
标签: reactjs