【发布时间】:2020-02-10 07:08:01
【问题描述】:
我有一个带有 Redux 商店的 React-Native 应用程序。我有一个提取请求,如果提取请求过长并显示错误消息,我想设置一个超时。
我的问题:如何设置超时并中止请求?因为否则在弹出错误消息并显示响应和成功消息后请求可能成功。
在 RN 0.60 中有 AbortController,但是如何将它与 redux 存储一起使用?还是有另一种带有布尔标志的方法? Redux 文档说:
一种方法是创建一个从你的 动作创造者。然后,只要触发“成功”动作 函数没有被调用。
你有这方面的例子吗?
我无法展示整个代码,但我尝试解释一下。
我的action有这个方法:
export function fetchData(type, id) {
return (dispatch, state) => {
return fetch(APIURL, {
method: 'GET',
headers: headers
})
.then /*handle Response and response errors */
}
}
我在我的组件中调用该函数:
<Dialog
action={async =>
{async () => {
try {
await this.props.fetchMyData();
await openUrlInBrowser(URL);
} catch (e) {
this.showError();
}
}}>
function mapDispatchToProps(dispatch: ThunkDispatch){
fetchMyData: () => {
return dispatch(fetchData("data"));
}
}
感谢您的帮助!
【问题讨论】:
标签: react-native redux fetch expo