react-native - 如何在反应中处理调度后导航
问题描述
我遇到了麻烦,因为在将数据放入商店react-navigation
后,我找不到导航到屏幕的方法。redux
我尝试做很多事情,但无济于事,问题一直存在。问题是redux
在屏幕弹出之前商店的调度速度不够快,然后屏幕出现错误,因为 json 文件的某些部分不存在。我想知道在使用必要的 JSON 文件更新商店之前是否会暂停导航。
这是我的应用程序中出现问题的部分。
_onPress = () => {
if (this.state.taxonA == "" || this.state.taxonB == "") {
Alert.alert("One or more fields have been left empty.");
return;
}
let url = V.sprintf(
"http://timetree.igem.temple.edu/api/pairwise/%s/%s",
this.state.taxonA,
this.state.taxonB
);
const { fetchData } = this.props;
fetchData(url);
console.log("should have fetched");
//while(this.props.response.isFetching == true){}
this.props.navigation.navigate("Picker");
};
这是fetchData
功能
export const fetchData = url => {
console.log("Should enter async dispatch");
return async dispatch => {
dispatch(fetchingRequest());
try{
let response = await fetch(url);
let json = await response.json();
let dispatchChecker = dispatch(fetchingSuccess(json));
console.log("DISPATCH", dispatchChecker);
console.log("JSON",json);
console.log(JSON.stringify(json, null, 2));
}catch(error){
console.log("ERROR",error);
dispatch(fetchingFailure(error));
}
};
};
输出应该是从一个屏幕到另一个屏幕的过渡。但相反,应用程序将获得一个红色屏幕,cannot read property of 'found_taxon_a' of null
其中包含以下语句:found_taxon_a 是获取的 JSON 文件中的一个对象。
解决方案
fetchData
在导航到另一个屏幕之前等待完成:
fetchData(url)
.then(() => this.props.navigation.navigate('Picker'))
.catch(err => console.log('Do something'));
推荐阅读
- c++ - 这段代码是什么意思“ofstream fout(getenv("OUTPUT_PATH"));”
- python - python 3 - 用负ndigits四舍五入十进制结果为科学记数法的十进制
- dns - DNS 响应 - 结合 A 和 TXT 记录
- github - 如何检查 GitHub PR 是否已被代码所有者批准?
- javascript - 如何在 Angular 5 中以编程方式填充数组
- javascript - RXJS 循环遍历项目并返回一个数组
- sql-server - SQL Server BCP 导出 SQL 字段中的逗号
- r - (R) 在相关矩阵热图中添加重要星
- hyperledger-composer - Hyperledger-Composer:条件类型为 (r.someArray.indexOf(p.getIdentifier()) > -1) 的 ACL 规则不起作用
- ios - 在不使用 CollectionView 的情况下在网格视图中列出 UIButton