在子组件之间共享父组件状态'州
Share parent state between child components' states?
学习React,我正在构建一个简单的计算器。组件模型为:
- Calculator
- Sliders (input[ref=cars])
- Dashboard (p {this.state.cars})
Sliders
是具有一组输入的元件,应通过母Calculator
状态转移到Dashboard
。
我的目标是使Dashboard
的状态值随着Sliders
的输入分量的变化而变化。
var Calculator = React.createClass({
getInitialState: function() {
return {
cars: 0
};
},
render: function () {
return (
<div className="calculator">
<Sliders
cars={this.state.cars}
/>
<Dashboard
cars={this.state.cars}
/>
</div>
)
}
});
var Dashboard = React.createClass({
getInitialState:function () {
return {
cars: this.props.cars
}
},
render: function () {
return (
<div>
<h1>Dashboard</h1>
<p>Cars: {this.state.cars}</p>
</div>
)
}
})
var Sliders = React.createClass({
getInitialState: function() {
return {
cars: this.props.cars
};
},
handleInput: function () {
var state = {
cars: this.refs.cars.value
}
this.setState(state);
},
render: function () {
return (
<div className="sliders">
<input type="text" ref="cars" onChange={this.handleInput} value={this.state.cars}/>
</div>
)
}
})
正如我所得到的,handleInput()
被触发,因此设置了Sliders
的状态。然而,它并没有"转移"到母公司Calculator
的"汽车"状态。因此,Calculator
的状态未更新=> Dashboard
的状态也未更新。
如何在滑块和仪表板之间共享计算器的父状态?
我知道,在SO上有很多类似的问题,但是很难找到具体的有用案例,特别是当你完全是React新手的时候。很抱歉重复了,
组件不共享状态。一个组件的状态可以成为另一个组件的道具。
在你的例子中,cars
应该是Calculator,
和Slide
的状态,Dashboard
应该只检索cars
作为道具。如果输入发生变化,Sliders
应该通知Calculator
,以便它可以更新其状态和呈现(这会导致Sliders
和Calculator
也进行更新):
var Calculator = React.createClass({
getInitialState: function() {
return {
cars: 0
};
},
onUpdate: function (cars) {
this.setState({cars});
},
render: function () {
return (
<div className="calculator">
<Sliders
cars={this.state.cars}
onUpdate={this.onUpdate}
/>
<Dashboard
cars={this.state.cars}
/>
</div>
)
}
});
var Dashboard = React.createClass({
render: function () {
return (
<div>
<h1>Dashboard</h1>
<p>Cars: {this.props.cars}</p>
</div>
)
}
})
var Sliders = React.createClass({
handleInput: function (event) {
this.props.onUpdate(event.target.value);
},
render: function () {
return (
<div className="sliders">
<input type="text" ref="cars" onChange={this.handleInput} value={this.props.cars}/>
</div>
)
}
})
应该尽可能少的组件具有状态,并且状态应该在树中尽可能高的位置。参见getInitialState中的Props是一个反模式
在React中,你希望有一个或多个"智能"组件,用于处理状态和"哑"组件。"智能"组件将状态传递给"愚蠢"组件。还注入事件处理程序,比如在您的例子中,用于更改汽车数量的处理程序。计算器是你的智能部件。它还应该有一个函数来处理状态变化。
handleCarsChange: function( newAmount ) {
this.setState({ cars: newAmount });
},
也不要让你的初始状态从props
(更多信息)。道具是可以改变的,直接在渲染函数中使用它们,像这样:
var Dashboard = React.createClass({
render: function () {
return (
<div>
<h1>Dashboard</h1>
<p>Cars: {this.props.cars}</p>
</div>
)
}
});
或者使用React 14组件函数语法:
var Dashboard = function(props) {
return (
<div>
<h1>Dashboard</h1>
<p>Cars: {props.cars}</p>
</div>
);
};
最后但并非最不重要的是给Sliders
组件一个处理函数来处理状态变化:
<Sliders
cars={this.state.cars}
handleCarsChange={this.handleCarsChange}
/>
并更改Slider
组件中的handleInput
功能:
handleInput: function(value) {
this.props.handleCarsChange(this.refs.cars.value);
},
将parent作为prop传递给slider,这样你就不需要cars作为slider的状态了。
这是一种方法,但FelixKing的答案更好。
var Calculator = React.createClass({
getInitialState: function() {
return {
cars: 0
};
},
handleInput: function () {
var state = {
cars: this.refs.cars.value
}
this.setState(state);
},
render: function () {
return (
<div className="calculator">
<Sliders
cars={this.state.cars} parent={this}
/>
<Dashboard
cars={this.state.cars}
/>
</div>
)
}
});
var Dashboard = React.createClass({
getInitialState:function () {
return {
cars: this.props.cars
}
},
render: function () {
return (
<div>
<h1>Dashboard</h1>
<p>Cars: {this.state.cars}</p>
</div>
)
}
})
var Sliders = React.createClass({
getInitialState: function() {
return {};
},
handleInput: function (value) {
this.props.parent.handleInput(this.refs.cars.value);
},
render: function () {
return (
<div className="sliders">
<input type="text" ref="cars" onChange={this.handleInput} value={this.props.cars}/>
</div>
)
}
})
- 从组件状态的数组中删除元素
- Reactjs:在修改对象之前,是否需要复制处于组件状态的对象
- 在Redux中使用组件状态是反模式的吗
- 如何处理需要子组件状态的Meteor数据
- 单击按钮更改组件状态
- 当登录状态为组件状态时,如何使用react路由器实现依赖登录的路径
- 我是否必须在 React 中使用 this.state 来维护组件状态
- 如何使用react路由器将顶级组件状态传递给Routes
- React组件状态与道具动态输入
- 在子组件之间共享父组件状态'州
- 当父组件状态改变时,如何强制子组件重新呈现
- 有条件地绑定组件状态
- js:创建一个多选择组件.选项组件状态的组合流不输出改变的状态
- 从props中初始化组件状态
- 为什么不将redux存储状态存储在本地组件状态中呢?
- 将Facebook Web SDK与ReactJS组件状态集成
- 当从父组件状态传递时,React prop没有在正确的时间更新
- 如何将包含组件'状态的值传递回父组件
- 检查事件函数中的组件状态是否有效
- 当值被分配给组件状态时,为什么console.log打印之前的状态