reactjs - 导航到屏幕或组件时刷新屏幕或组件
问题描述
我有两个屏幕,一个用于显示使用 API 的记录,另一个用于注册。
问题是,当我进行注册并导航到显示屏幕时,它不会更新。
这是屏幕的构造:
constructor(props) {
super(props);
this.state = {isLoading: true, pendIsLoading: true, dataSource: [], contentStorageS:""}
};
fetchDados = async () => {
let usuario = await AsyncStorage.getItem("ASCOFAR_app_usuario");
try {
const response = await api.get("api/listaRegistros.php?usuario="+usuario);
const responseData = await response.data
if(responseData.status == "ERRO"){
this.setState({
isLoading: false,
dataSource: "",
})
}else{
this.setState({
isLoading: false,
dataSource: responseData,
})
}
console.log(response)
} catch (error) {
Alert.alert(error)
}
}
async componentDidMount () {
this.fetchDados();
this.atualizaState();
}
tirarLoad() {
if(this.state.isLoading == true){
return (
<ActivityIndicator size="large" color="#be152c"/>
)
}else if(this.state.dataSource == ""){
return (
<ScrollView >
<View style={{justifyContent:"center", alignItems:"center",}}>
<Image
style ={{width:150, height:150, marginTop:35}}
source={require('../assets/images/aguardando.png')}
/>
</View>
</ScrollView>
)
}else{
return (
<ScrollView>
<Text style={styles.tituloGrid}>Formularios Enviados</Text>
{this.state.dataSource.map(dados => (
<View style={styles.list} key={dados.id}>
<Text style={styles.listChild}>{dados.id}</Text>
<Text style={styles.listChild}>{dados.nome}</Text>
<Text>|</Text>
<Text style={styles.listChild}>{dados.endereco}</Text>
</View>
))}
</ScrollView>
)
}
}
<View style={styles.grid}>
{this.tirarLoad()}
</View>
导航到此屏幕以更新 API 消耗时,我需要知道该怎么做
解决方案
尝试在导航后重新渲染主屏幕
this.props.navigation.navigate('Home', {
onBack: () => this.refresh() //function to refresh screen,
});
推荐阅读
- git - 我只是为 Git 上的同一个远程仓库添加了两个别名。我怎样才能删除其中之一?这是我的代码
- browser - 将浏览器最小缩放级别设置为低于 25%
- json - 在 Haskell ghci 中输入 JSON 文字的最简单方法
- jquery - Ajax jquery加载图像的本地mac文件夹:匹配未定义的错误
- c++ - 如何在 qml 上绘制高性能的形状
- wagtail - 使用外键集编辑站点设置时生成 KeyError
- php - 自定义帖子类型永久链接不适用于分层父级
- android-sqlite - 如何修复“java.lang.IllegalStateException:无法从 CursorWindow 读取第 2 行,第 7 列。”
- java - JPEG 文件无法在 BlueJ 中正确加载
- amazon-web-services - 使用 Mup 部署和 NGINX 的 Meteor 应用程序未出现在 AWS 上