Javascript React redux不';t返回当前状态值

Javascript React redux不';t返回当前状态值,javascript,reactjs,redux,state,thunk,Javascript,Reactjs,Redux,State,Thunk,我对react中的redux thunk有问题。在thunk文件中,我使用从reducer中分派操作并更改isSuccess和isLoading标志,但是在组件中,在API调用之后,我得到了这些操作的前一个值,而不是当前值 组件.jsx wait addClient(valueToSubmit,employee.tenantId)。然后((响应)=>{ 如果(isSuccess){ showToast(toastTypes.SUCCESS,翻译(“客户添加成功”); resetForm(); 设

我对react中的redux thunk有问题。在thunk文件中,我使用从reducer中分派操作并更改isSuccess和isLoading标志,但是在组件中,在API调用之后,我得到了这些操作的前一个值,而不是当前值

组件.jsx

wait addClient(valueToSubmit,employee.tenantId)。然后((响应)=>{
如果(isSuccess){
showToast(toastTypes.SUCCESS,翻译(“客户添加成功”);
resetForm();
设置提交(假);
切换模式();
setFieldsWithError([]);
}否则{
展示土司(
toastTypes.ERROR,
message.debugMessage?包括(“存在”)
?翻译(“客户存在错误”)
:translate(“客户端添加错误”)
);
setFieldsWithError(message.payload);
}
});
thunk.js

export const addClient=(数据,租户)=>async(分派)=>{
const tenantId=tenant?.replace(/['“]+/g,”);
分派(requestAddClient());
返回等待api(
“职位”,
阿皮乌尔,
数据,
租户?{“租户uuid”:租户}:{},
假的
)
。然后((响应)=>{
返回调度(receiveAddClient(response.data.payload));
})
.catch((错误)=>{
返回调度(errorAddClient(err.response));
});
};
actions.js

export const requestAddClient=()=>{
返回{
类型:请求\添加\客户端,
};
};
导出常量receiveAddClient=(客户端)=>{
返回{
类型:接收\添加\客户端,
客户:客户,,
};
};
导出常量errorAddClient=(消息)=>{
返回{
类型:错误\u添加\u客户端,
消息
};
};
reducer.js

案例请求\u添加\u客户端:
返回{
……国家,
客户:{},
isSuccess:false,
孤岛加载:是的,
};
案例接收\添加\客户端:
返回{
……国家,
客户:action.client,
客户端:[…state.clients,action.client],
isSuccess:对,
孤岛加载:false,
};
案例错误\u添加\u客户端:
返回{
……国家,
客户:{},
消息:action.message.data,
isSuccess:false,
孤岛加载:false,
};

我无法解决这个问题。如果有人知道原因,请告诉我。提前谢谢!

在addClient thunk中,您可以在catch中重新播放

.catch((err) => {
  dispatch(errorAddClient(err.response));
  return Promise.reject(err.response.data)
});
在组件中,您可以执行以下操作:

await addClient(valueToSubmit, employee.tenantId).then(
  (response) => {
    showToast(
      toastTypes.SUCCESS,
      translate('client_add_success')
    );
    resetForm();
    setSubmitting(false);
    toggleModal();
    setFieldsWithError([]);
  },
  (message) => {
    //promise was rejected
    showToast(
      toastTypes.ERROR,
      message.debugMessage?.includes('exist')
        ? translate('client_exist_error')
        : translate('client_add_error')
    );
    setFieldsWithError(message.payload);
  }
);

另一种(更好的)方法是使用
useffect
使用
useSelector
检查来自状态的请求状态,并根据该值触发需要执行的操作。

感谢您的回答,但我想我根本不明白我必须做什么。在useffect中我必须做什么?