【发布时间】:2016-09-09 00:37:15
【问题描述】:
我正在开发一个应用程序,在该应用程序中我使用流中传入的数据更新接口。我从类似这样的代码开始:
var TempDisplay = React.createClass({
getInitialState: function() {
return {
tempFAA:0.69,
tempGGG:1.4,
temp03K:3.66,
temp60K:58.79,
rateFAA:-0.04,
rateGGG:-0.0001,
rate03K:0.45,
rate60K:1.3,
}
},
render: function() {
return(
<div>
<Temp label="60K" color="#d62728" temp={this.state.temp60K} rate={this.state.rate60K} />
<Temp label="03K" color="#2ca02c" temp={this.state.temp03K} rate={this.state.rate03K} />
<Temp label="GGG" color="#ff7f0e" temp={this.state.tempGGG} rate={this.state.rateGGG} />
<Temp label="FAA" color="#1f77b4" temp={this.state.tempFAA} rate={this.state.rateFAA} />
</div>
)
}
});
var Instrument = React.createClass({
render: function() {
return(
<div><span style={{color:this.props.color}}>{'\u25C9 '}</span>{this.props.label}</div>
)
}
});
var tempDisplay = ReactDOM.render(<TempDisplay />, document.getElementById("tempDisplay"));
我现在应该做的是调用tempDisplay.setState() 以在有新数据时更新这些温度显示,但tempDisplay 似乎是一个<div> 对象,无法调用setState()。我应该如何正确地做到这一点?
【问题讨论】:
-
你所说的“当新数据进来时”是什么意思,你的状态中的数据会改变的唯一方法是如果你用新的值替换它......你期望这会发生在哪里from react 很棒,但不是魔法
-
也许最好不要
setState的tempDisplay组件,而是使用道具将数据传递给它?如果数据在外部更新,则不需要使用组件状态来存储。 -
就像我上面说的,数据以流的形式传入。所以不,不是其他 React 组件。
标签: javascript reactjs jsx