Javascript 如何从redux获取承诺的状态?
我在react本机应用程序中使用redux。为了更新一些配置文件数据,我在我的一个组件中发送一个actionupdateprofile(数据)Javascript 如何从redux获取承诺的状态?,javascript,reactjs,redux,Javascript,Reactjs,Redux,我在react本机应用程序中使用redux。为了更新一些配置文件数据,我在我的一个组件中发送一个actionupdateprofile(数据) values = { // some Js object values } updateProfile(values) 在我的组件中 values = { // some Js object values } updateProfile(values) 行动中的创造者 export const updateProfile = (details) =&
values = { // some Js object values }
updateProfile(values)
在我的组件中
values = { // some Js object values }
updateProfile(values)
行动中的创造者
export const updateProfile = (details) =>(dispatch) => {
dispatch(profileLoading())
axios.post(SERVERURL,details) //updating the details to the server
.then((result)=>{
dispatch(addProfile(result.data[0])) //updating the returned details to redux state
})
.catch((err)=>{
dispatch(profileFailed(err.response))
})
})
}
export const profileLoading = () => ({
type: ActionTypes.PROFILE_LOADING,
})
export const addProfile = (profile) => ({
type: ActionTypes.ADD_PROFILE,
payload: profile
})
export const profileFailed = (err) => ({
type: ActionTypes.PROFILE_FAILED,
payload: err
})
profile.js
import * as ActionTypes from './ActionTypes'
export const profiles = (state = {
isLoading:true,
errMess:null,
profiles:{ }
},action) =>{
switch(action.type){
case ActionTypes.ADD_PROFILE:
return {...state, isLoading:false, errMess:null, profiles: action.payload}
case ActionTypes.PROFILE_LOADING:
return {...state, isLoading:true,errMess:null}
case ActionTypes.PROFILE_FAILED:
return {...state, isLoading:false,errMess:action.payload, profiles: {}}
case ActionTypes.DELETE_PROFILE:
return {...state,isLoading:false,errMess:null,profiles:{}}
default:
return state
}
}
在这一行updateProfile(values)之后,当前我使用下面的setTimeout来了解更新的结果
updateProfile(values)
setTimeout(()=>{
if(profiles.errMess==null){
setCondition('completed')
nav.navigate('some Screen')
}
else{
setCondition('error')
}
},3000)
由于我需要导航到其他屏幕,我不能使用SetTimeOut,因为即使更新很快完成,它也会不断产生延迟,如果更新需要3秒钟以上,它仍然会引起头痛。我想要的是,仅当数据更新到服务器时(如承诺),才导航到“某个屏幕”。是的,我可以在redux状态中更新状态值,但我需要在组件级别实现它。我是新来的。请有人帮助我。我相信您可以返回axios请求,以访问您所称的promise 您可以看到一个讨论,讨论您在这里面临的相同挑战:
//操作创建者
导出常量更新配置文件=(详细信息)=>(调度)=>{
分派(profileLoading())
返回axios.post(服务器URL,详细信息)
.然后((结果)=>{
分派(addProfile(结果数据[0]))
}).catch((错误)=>{
分派(配置文件失败(错误响应))
})
})
}
//用法
分派(更新配置文件(值))。然后((响应)=>{
//处理成功的响应
}).catch((错误)=>{
//处理失败
})