【问题标题】:React redux update state onClick function with multiple actions用多个动作反应 redux 更新状态 onClick 函数
【发布时间】: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


【解决方案1】:

现在您的问题已经更清楚了,看起来您正在寻找一种在第一个操作已经完成后调度第二个操作的方法。

我可能会以这种方式使用componentWillReceiveProps

componentWillReceiveProps(nextProps){
    const {filters: {active_case_page}, getCases} = nextProps;
    if (active_case_page !== this.props.filters.active_case_page){
      getCases(active_case_page,'All','dateCreated',false,null,'5a9ee9fa88406eeeebabbd1f'
    }
}

但是 IMO,整个模式不是很好。 我会将这两个动作放在一个动作中并一起处理所有事情(除非您的问题中缺少功能,因为目前我看不出有任何理由将此动作分为两个动作)

【讨论】:

  • 感谢您的输入,正确,我正在尝试在第一个操作完成后调度第二个操作。我同意在这种情况下应该一起处理这两个功能。但是,可以说有更多功能我必须分离操作,使用 componentWillReceiveProps 是实现同步执行的唯一方法
  • 因为你调度的第一个动作是一个只改变你的本地状态而不是对服务器的请求的动作,这就是我推荐的方式。如果您正在等待来自服务器的响应以调度另一个操作,那么我建议您使用承诺/异步等待,但目前这不是问题......
猜你喜欢
  • 2019-10-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-22
  • 2020-04-19
  • 1970-01-01
  • 2022-01-23
  • 1970-01-01
相关资源
最近更新 更多