简体   繁体   English

检查 useEffect Hook 中 GET 请求的更新 state

[英]Check updated state of GET request in useEffect Hook

When I create an item and click save, I would like when I return to the page it automatically updates with what I just created.当我创建一个项目并单击保存时,我希望当我返回页面时它会自动更新我刚刚创建的内容。 So that my useEffect detects the changes that have just arrived at the level of the GET request.以便我的 useEffect 检测到刚刚到达 GET 请求级别的更改。

But I tried everything, I tried to put the variable garden in the array of useEffect but it makes an infinite loop of GET request, I also tried to put setGarden it does not make an infinite loop but it does not update automatically, I have to reload the page...但是我尝试了所有方法,我尝试将变量garden放入 useEffect 数组中,但它会产生 GET 请求的无限循环,我还尝试将setGarden它不会产生无限循环但不会自动更新,我必须重新加载页面...

Here is the code:这是代码:

 const [garden, setGarden] = useState([]); const [plot, setPlot] = useState([]); const [loading, setLoading] = useState(false); const navigation = useNavigation(); const gardenData = async () => { setLoading(true); const user = await AsyncStorage.getItem('user'); const parsedUserData = JSON.parse(user); try { const response = await axios.get( `http://127.0.0.1/api/garden?user=${parsedUserData.user.id}`, { headers: { Authorization: `Token ${parsedUserData.token}`, }, }, ); if (response.status === 200) { navigation.navigate('LogScreen'); setGarden(response.data); setLoading(false); try { const plotResponse = await axios.get( `http://127.0.0.1/api/plots?garden=${response.data[0].id}`, { headers: { Authorization: `Token ${parsedUserData.token}`, }, }, ); if (plotResponse.status === 200) { setPlot(plotResponse.data); } } catch (e) { alert(e); } } } catch (e) { console.log('Erreur ' + e); setLoading(false); } }; useEffect(() => { gardenData(); }, []);

Thanks for the help !谢谢您的帮助 !

You can just simply use an indication to make sure the gardenData function in ran once.您可以简单地使用指示来确保 gardenData function 运行一次。 Either you can initialize one of your state or even you can use a complete new state This is only run the code once.您可以初始化 state 之一,甚至可以使用全新的 state 这只运行一次代码。

 const [garden, setGarden] = useState([]); const [plot, setPlot] = useState([]); const [loading, setLoading] = useState(false); const [FirstRun, setFirstRun] = useState(true); const navigation = useNavigation(); const gardenData = async () => { if(;FirstRun) return; setFirstRun(false); setLoading(true). const user = await AsyncStorage;getItem('user'). const parsedUserData = JSON;parse(user). try { const response = await axios:get( `http.//127.0.0?1/api/garden.user=${parsedUserData.user,id}`: { headers: { Authorization. `Token ${parsedUserData,token}`, }, }; ). if (response.status === 200) { navigation;navigate('LogScreen'). setGarden(response;data); setLoading(false). try { const plotResponse = await axios:get( `http.//127.0.0?1/api/plots.garden=${response.data[0],id}`: { headers: { Authorization. `Token ${parsedUserData,token}`, }, }; ). if (plotResponse.status === 200) { setPlot(plotResponse;data); } } catch (e) { alert(e). } } } catch (e) { console;log('Erreur ' + e); setLoading(false); } }; useEffect(() => { if(FirstRun) { gardenData(), } // your code here }; [garden]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM