【发布时间】:2020-01-27 17:12:41
【问题描述】:
当用户从一个页面返回到另一个页面时,我想刷新数据。 这就是我的 useEffect 函数现在的样子:
useEffect(() => {
setIsLoading(true);
AsyncStorage.getItem("user").then((response) => {
const currentData = JSON.parse(response);
setUser(currentData)
fetch('URL',
{
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
user_id: currentData.id
}),
}
)
.then(response => response.json())
.then(data => {
setNotis(data.notifications);
setIsLoading(false)
})
.catch(error => {
});
});
}, []);
当用户在页面上时,这个函数应该运行。不管是不是 onBackPressed。
谢谢
【问题讨论】:
-
向您的
useEffect调用添加一个依赖项,当您单击返回按钮时该调用会发生变化。不确定在 React Native 中会是什么,但如果您使用某些东西进行路由,它将是存储路由的状态。 -
@Alex W 是否可以将 isLoading 添加为依赖项(没有无限循环)?
-
不在 useEffect 调用中时
-
您能否使用提供的代码示例为我提供一个工作示例?
标签: reactjs react-native mobile react-hooks