如何根据数据更新状态前后调用函数-React-redux [英] How to call a function before and then depending on data update state-React-redux

查看:71
本文介绍了如何根据数据更新状态前后调用函数-React-redux的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个下拉菜单选择传奇被调用,并且必须使用相同的组件.saga 被调用,唯一没有发生的事情是 set 状态在 saga 调用之前更新,因此组件从不更新数据.

I've a drop down on select the saga is called and the same component must be used. The sagas are called the only thing not happening is the set state is updating before the saga call therefore the component never updates the data.

            recievedChangeValue=(selectVal)=>{
                console.log(selectVal)
                if(selectVal==='Yearly'){
                     this.props.getYearlySales() **//call to saga**
                     this.setState({salesData:this.props.yearlySales}) **//it updates before the called saga ends**                         
                 }
                if(selectVal==='Decade'){ 
                    this.props.getSales()**//call to saga**
                    this.setState({salesData:this.props.dataSales})   **//it updates before the called saga ends**                                           
                }
            }

我知道回调,但这里的状态只能在传奇调用之后更新.我从过去一天开始就在处理它,我不知道必须做什么.任何帮助表示赞赏.请让我知道我哪里出错了.

I know the callback but here the state must be updated only after the saga call.I'm working onto it since past day I've no idea as to what has to be done. Any help is appreciated.Please lemme know as to where i'm going wrong.

推荐答案

 recievedChangeValue=(selectVal)=>{
this.setState({selectedSalesData:selectVal},
  ()=>{
    if(selectVal==='Yearly'){
        this.props.getYearlySales() 
    }
    if(selectVal==='Decade'){ 
        this.props.getSales()
    }
  }
)

}

以下是我在render中写的

The following I wrote in render

   let SalesData=this.handleSalesData(selectedSalesData)//calls on selecteddata and it works like a charm

这篇关于如何根据数据更新状态前后调用函数-React-redux的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆