不確定我曾經在一個文件中的下列組件調用DashboardPosition.js
:componentInstance.setState在0.11.0
var DashboardPosition = React.createClass({
getInitialState: function() {
return {
items: []
};
},
render: function() {
return (
<div className="well well-sm dashboard-item">
This component has {this.state.items.length} items
</div>
);
}
});
然後,我在我的網頁沒有問題,像這樣使這個:
var dashboardPosition = <DashboardPosition />;
React.renderComponent(dashboardPosition, document.querySelector("#position"));
但當我嘗試以下操作:
var dashboardPosition = <DashboardPosition />;
React.renderComponent(dashboardPosition, document.querySelector("#position"));
dashboardPosition.setState({
items: [{name: "AMD"}, {name: "LIQ"}, {name: "ELEC"}]
});
我收到以下錯誤:
Uncaught TypeError: undefined is not a function
dashboardPosition.setState
這似乎只發生在v0.11.0
。我已經在v0.10.0
試過了,它工作正常。可能的錯誤或我錯過了什麼?
好吧,謝謝,我想我需要通過它作爲屬性然後:) – CallumVass
在這種情況下,你可以做'React.renderComponent( ,target)'當你想更新它。每次嘗試不要執行document.querySelector。 –
FakeRainBrigand