简体   繁体   English

为什么我的 React setstate 没有立即更新?

[英]why my React setstate is not updating immediately?

This a my function这是我的 function

    onSelectAction = (x, o) => {
    var { takeActionsOptions } = this.props.main
    console.log(o, "onSelectAction")
    var tempAction=_.cloneDeep(takeActionsOptions)

    _.keys(tempAction).map(a => {
        if(a === x.processCode) {
            tempAction[a].map((b)=>{
                b.isSelected = b.id === o.id

            })
        }
        else{
            tempAction[a].map((b)=>{
                b.isSelected = b.id === 0
            })
        }
    })
    StoreActions.setState({ takeActionsOptions:tempAction});
    this.onClickTakeAction(o, x)
}

where tempAction is changing the property like the i wanted to. tempAction 正在像我想要的那样更改属性。 But when im trying update the store... this { takeActionsOptions:tempAction} is not getting updated for the first time.但是当我尝试更新商店时...这 { takeActionsOptions:tempAction} 不是第一次更新。 After 2-3 clicks on the desired location this is getting updated.在所需位置上单击 2-3 次后,这将得到更新。 i want to update immediately in the store because there is another function which fetches data from the store and does another operation.我想在商店中立即更新,因为还有另一个 function 从商店中获取数据并执行另一个操作。

this is my other function which is using the take "takeActionsOptions " from store.这是我的另一个 function,它正在使用商店中的“takeActionsOptions”。 so if that function is not updating then this function isnt working properly所以如果 function 没有更新那么这个 function 就不能正常工作

    onClickTakeAction = (o, x) => {
    var { takeActionsOptions=[] } = this.props.main
    var selectedAction = takeActionsOptions[x.processCode].find(a => a.isSelected)
    if (selectedAction.id === 0) {
        hydro.msg.info("Please select an option.")
        return;
    }
    var tempAction=_.cloneDeep(takeActionsOptions)
    _.keys(tempAction).map(a => {
        tempAction[a].map((b)=>{
            b.isSelected = b.id === 0
        })
    })
    this.setState({takeActionsOptions:tempAction})

    switch (selectedAction.id) {
        case 1:
            var userName = somecode.userName;
            if (userName.toUpperCase() === x.userName.toUpperCase()) {
                Actions.deleteSelectedProcess(x);
            }
            else {
                somecode.info("Not your Process")
            }
            break;
        case 2:
            Action.downloadLogs(x);
            break;
       
    }
}
var tempAction=_.cloneDeep(takeActionsOptions)

What the cloneDeep function is doing here? cloneDeep function 在这里做什么? If it does any API calling/Getting data from the server, you need to wait for a moment to get the data.如果它做任何API calling/Getting data from the server,你需要稍等片刻才能拿到数据。 Meanwhile, you can disable the button and show some loaders for interactivity.同时,您可以禁用该按钮并显示一些加载程序以进行交互。

If you're using the loadash to deep copy the object, up to my knowledge loadash functions, takes a long time to complete based on the CPU or object you are trying to copy.如果您使用 loadash 深度复制 object,据我所知,loadash 函数需要很长时间才能完成,具体取决于您尝试复制的 CPU 或 object。 So try to wait for a minute and check whether it's updating or not.因此,请稍等片刻,然后检查它是否正在更新。 If it is updating, then you should disable the button until then.如果它正在更新,那么您应该在此之前禁用该按钮。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM