Reactjs 组件状态更改后更新Redux
仍然在学习正确使用Redux。我已经干了一个小时了,需要帮助。 我想通过有状态组件的子功能组件更新其状态。之后,我想激活一个类方法,它使用Redux操作来更新存储。我试图让这两件事按顺序发生,但在执行状态更改的方法解决之前,无法访问状态更改 当本地类状态更新后,我如何诱导redux操作激活 我的主要问题是handleMiniChange发生后状态会更新,因此存储中的完整日期在历史记录中总是落后一个事件(即,我需要与下拉列表交互两次,以便Redux注册第一个操作,然后它总是落后一步) 我尝试了这个.forUpdate和这个.componentWillUpdate,但我遇到了无限循环。(也许我做错了…?) 请注意,“update_field”是负责更新存储的redux操作 我的代码:Reactjs 组件状态更改后更新Redux,reactjs,redux,Reactjs,Redux,仍然在学习正确使用Redux。我已经干了一个小时了,需要帮助。 我想通过有状态组件的子功能组件更新其状态。之后,我想激活一个类方法,它使用Redux操作来更新存储。我试图让这两件事按顺序发生,但在执行状态更改的方法解决之前,无法访问状态更改 当本地类状态更新后,我如何诱导redux操作激活 我的主要问题是handleMiniChange发生后状态会更新,因此存储中的完整日期在历史记录中总是落后一个事件(即,我需要与下拉列表交互两次,以便Redux注册第一个操作,然后它总是落后一步) 我尝试了这个
function MiniDropDown (props) {
return (
<select name={props.name} value={props.value} onChange={props.handleMiniChange}>
{
props.list.map((obj) => {
return <option key={obj.Value} value={obj.Value}>{obj.Value}</option>
})
}
</select>
)
}
/*************************************************
* MAIN COMPONENT
*************************************************/
export default class DateMenu extends React.Component {
constructor(props) {
super(props);
this.state = {
month:'MM',
day:'DD',
year:'YYYY',
}
this.handleDateChange = this.handleDateChange.bind(this)
this.handleMiniChange = this.handleMiniChange.bind(this)
}
handleMiniChange(event) {
const name = event.target.name;
const value = event.target.value;
this.setState({ [name]: value});
this.handleDateChange()
}
handleDateChange () {
const _date = `${this.state.month}/${this.state.day}/${this.state.year}`;
this.props.common_props.update_field(this.props.name, _date);
}
render() {
const common_props= {
form: this.props.common_props.form_data,
handleMiniChange: this.handleMiniChange,
}
return (
<div className='traveler_data_row'>
<label> {this.props.specs.label} </label>
<MiniDropDown value={this.state.month}
name='month'
list={getMonths()}
{...common_props} />
<MiniDropDown value={this.state.day}
name='day'
list={getDays()}
{...common_props} />
<MiniDropDown value={this.state.year}
name='year'
list={getYears()}
{...common_props} />
</div>
)
}
}
功能小下拉列表(道具){
返回(
{
道具列表地图((obj)=>{
返回{obj.Value}
})
}
)
}
/*************************************************
*主要成分
*************************************************/
导出默认类DateMenu扩展React.Component{
建造师(道具){
超级(道具);
此.state={
月份:'MM',
日期:'DD',
年份:'yyy',
}
this.handleDateChange=this.handleDateChange.bind(this)
this.handleMiniChange=this.handleMiniChange.bind(this)
}
handleMiniChange(事件){
const name=event.target.name;
常量值=event.target.value;
this.setState({[name]:value});
this.handleDateChange()
}
handleDateChange(){
const_date=`${this.state.month}/${this.state.day}/${this.state.year}`;
this.props.common_props.update_字段(this.props.name,_date);
}
render(){
const common_props={
表单:this.props.common_props.form_数据,
handleMiniChange:this.handleMiniChange,
}
返回(
{this.props.specs.label}
)
}
}
这里的问题是setState
是一个异步操作。
这意味着在调用setState
后,您无法确定状态何时真正更新,以及何时可以在其他处理程序中使用新状态。
一种解决方案是使用setState
并调用回调中的第二个处理程序
this.setState({ params }, this.secondHandler)
是的,这就解决了!我确信它比我的onClick
解决方案要好,thnx!读这个答案,