最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Why is my useEffect() not running on change or page refresh - React Js? - Stack Overflow

programmeradmin3浏览0评论

The Firebase query I have created is working perfectly fine as it returns the correct data. I have access to the correct information, but my table is not displaying the data because the function getUsers() is not running by default or even a page refresh. I am still new with React js, so take it easy on me please lol

The Firebase query I have created is working perfectly fine as it returns the correct data. I have access to the correct information, but my table is not displaying the data because the function getUsers() is not running by default or even a page refresh. I am still new with React js, so take it easy on me please lol

Share Improve this question edited Feb 1, 2021 at 9:34 Emil Gi 1,1785 silver badges12 bronze badges asked Jan 30, 2021 at 20:58 CodingBoyCodingBoy 591 gold badge1 silver badge10 bronze badges 5
  • Do you put those useEffect after the return statement? – kunquan Commented Jan 30, 2021 at 21:34
  • UseEffect and useState should be at the top level. I think the function getUsers is also wrong. setDetails(obj) should be outside the foreach function getUsers () { query.forEach() { your code} } – jShubh Commented Jan 30, 2021 at 21:42
  • function getUsers () { query.forEach() { your code} setDetails(obj) } }` – jShubh Commented Jan 30, 2021 at 21:49
  • 1 Please don't post screenshots of your code, or other textual content. Instead post the actual text, and use the formatting tools of Stack Overflow to mark it up. – Frank van Puffelen Commented Jan 30, 2021 at 21:55
  • Hi, will do that in the future my apologies. The problem was that the setDetails() was in the wrong place lol – CodingBoy Commented Jan 30, 2021 at 22:16
Add a ment  | 

2 Answers 2

Reset to default 3

Try

useEffect(() => getUsers(),[Details]);

Adding your state as the dependency should allow it run every time the state changes

The useEffect hook with an empty dependency array will only run in the onMount lifecycle phase of the ponent, that means only once. If you want it to run on every render, remove the dependency array like:

useEffect(() => getUsers());
发布评论

评论列表(0)

  1. 暂无评论