A component cannot update its own props unless they are arrays or objects (having a component update its own props even if possible is an anti-pattern), but can update its state and the props of its children.
(组件不能更新自己的道具,除非它们是数组或对象(即使可能是一个反模式,组件更新自己的道具),但可以更新其状态和其子项的道具。)
For instance, a Dashboard has a speed
field in its state, and passes it to a Gauge child thats displays this speed.
(例如,仪表板在其状态下具有speed
字段,并将其传递给显示此速度的Gauge子项。)
Its render
method is just return <Gauge speed={this.state.speed} />
.(它的render
方法只是return <Gauge speed={this.state.speed} />
。)
When the Dashboard calls this.setState({speed: this.state.speed + 1})
, the Gauge is re-rendered with the new value for speed
.(当仪表板调用this.setState({speed: this.state.speed + 1})
,将使用新的speed
值重新渲染Gauge。)
Just before this happens, Gauge's componentWillReceiveProps
is called, so that the Gauge has a chance to compare the new value to the old one.
(就在此之前,调用Gauge的componentWillReceiveProps
,以便Gauge有机会将新值与旧值进行比较。)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…