reactjs - React 异步动作调度
问题描述
在我的反应应用程序中,我遇到了异步等待操作的问题。
这是我的代码。
export const createOrUpdateProfile = (profileData, history) => async dispatch => {
try {
dispatch(setProfileLoading());
await axios.post('/api/profile', profileData);
history.push('/dashboard');
} catch(error) {
dispatch(profileError(GET_ERRORS, error.response.data));
}
}
问题是加载微调器在短暂延迟后显示。我用 . 将loading
(a bool) 设置为 true setProfileLoading()
。加载仪表板时,仅基于profileLoading
布尔值显示微调器。
但在导航到仪表板之前,会调度一个异步操作来创建配置文件:
await axios.post('/api/profile', profileData);
然后只发生导航:
history.push('/dashboard');
为了克服这个问题,我将history.push('/dashboard')
调用放在异步操作之前。我这样做对吗?在进行 API 调用之前进行导航似乎有点奇怪。有人可以帮我吗?
export const createOrUpdateProfile = (profileData, history) => async dispatch => {
try {
dispatch(setProfileLoading());
history.push('/dashboard');
await axios.post('/api/profile', profileData);
} catch(error) {
dispatch(profileError(GET_ERRORS, error.response.data));
}
}
解决方案
推荐阅读
- azure - 如何将 http 请求 URL 中的路径剥离为 Azure CDN 标准规则的重写或重定向 URL?
- javascript - 如何在 React 中使用变量?
- r - 变异期间的递归滞后()?
- python-3.x - 如果一列中的值在另一列中有多个值,如何在pandas中根据优先级进行过滤
- linux - 模块加载期间出现错误“未知符号 __copy_to_user”
- python - 如果两个单元格值与 pandas 中另一个较小的子集数据框匹配,则使用 True 填充新的数据框列
- python - 为每个 id 创建额外的行
- database - IBM Cloudant 容量
- django - Django Annotate ExpressionWrapper 不返回预期类型
- python - 搜索结果的分页仅在 Django 中不起作用