【发布时间】:2018-08-29 10:13:38
【问题描述】:
我正在尝试更新 redux 状态,我遇到的问题是当使用多个操作执行 onclick 函数时 mapStateToProps 没有被更新。我知道动作是异步运行的,这就是状态没有按时更新的原因。有没有办法克服这个问题
代码示例
执行 OnClickFunction 时,getCase 操作将 this.props.filters.active_case_page 作为页码 并且 nexPage 操作更改了 active_case_page 索引,但这里的问题是 this.props.getCases 在执行 getCase 操作时仍然具有 this.props.filters.active_case_page 的旧值
OnClickFunction = () => {
this.props.nextPage(this.props.filters.active_case_page);
this.props.getCases(this.props.filters.active_case_page,'All','dateCreated',false,null,'5a9ee9fa88406eeeebabbd1f')
}
function mapStateToProps(state){
return{
filters: state.doctors.filters,
}
}
function mapDispatchToProps(dispatch){
return bindActionCreators({
nextPage:nextPage,
getCases:getCases
},dispatch)
}
export function nextPage(){
return{
type:"NEXT_PAGE"
}
}
export function getCases(pageNum,filterType,filterOption,isSearchOn,searchText,doctor_id){
return function(dispatch){
axios.get("/api/getCases/"+pageNum+"/"+filterType+"/"+filterOption+"/"+isSearchOn+"/"+searchText+"/"+doctor_id)
.then(function(response){
dispatch({type:"GET_CASES",payload:response.data});
})
.catch(function(err){
dispatch({type:"GET_CASES_REJECTED",payload:err});
})
}
}
export function doctorsReducers(state={
customer_cases:[],
filters:{
active_case_page:0,
selected_cases_filter:'dateCreated',
filter_options: 'All',
isCaseSearchOn:false,
search_cases:'',
}
}, action){
switch(action.type){
case "NEXT_PAGE":
// return the state and copy of boos array from state
return {
...state,
filters:{
...state.filters,
active_case_page:state.filters.active_case_page+1,
}
}
break;
case "GET_CASES":
// return the state and copy of boos array from state
return {
...state,
customer_cases:[...action.payload[0]],
}
break;
}
return state;
}
【问题讨论】:
-
我无法理解您遇到的问题是什么,您可以编辑您的问题并更具体一些吗?你想达到什么目的?
-
您的示例代码中没有 mapStateToProps 函数。
-
问题已更新
-
阅读 3 遍,也没有遇到您的问题。你的组件是什么样子的?
标签: javascript node.js reactjs redux react-redux