In React useEffect does not update after the value has changed from other component

前端 未结 1 1571
清酒与你
清酒与你 2021-01-22 05:46

I am trying to do some kind of online shop for myself and I got a problem. I want to render my shopping cart size in the NavBar component (which is on every page).<

相关标签:
1条回答
  • 2021-01-22 06:01

    In the dependency array you need to pass only the name of the function, just like below:

    useEffect(() => {
       setCount(getTotalCount());
       console.log('count useEffect');
    }, [getTotalCount]); // not like => getTotalCount()
    

    The code was calling also the function like getTotalCount() but only the name is needed, you don't need to call that.

    I hope that helps!

    0 讨论(0)
提交回复
热议问题