Bro I'm an experienced react dev so I already know most of the stuff you talk about. But you are probably the best coding RU-vidr on the platform, no yelling mild takes, not pushing an agenda, just clean, precise and interesting content. Keep it up!
That probably explains why I get the api calls with react query running twice on mount, good thing I started using the signal to abort the request on cleanup. I was aware of React's strict mode, but never understood it correctly, this video made it very clear, thank you! And I think strict mode is a must, very useful.
Thanks! I often misunderstand the whole concept of strict mode. One time I was fetching the data multiple times and end up wasting 1 day only to find out it was happening bcz of the strict mode tho:)
I’ve always enjoyed your streams while you’re working on Mirage. When I get a notification for a new video from you, I get really excited, and you never disappoint. Keep on going! Your videos are insanely good. Quality content, superb delivery. Only a if you make them more often.
Sheesh this was really great! I knew about the "cleanup" return function in useEffect, but I have probably done that mistake 100 times in my apps, and never noticed 😅 Also - the backlight doubling as practical light in the background is 🧑🍳😘
The proper way to implement this is to instead use setTimeout and add count as a dependency, so that when count is updated it restarts the timeout. setInterval should never be used as it does not guarantee execution if the inner function takes longer than the interval. (there are other issues with setInterval as well - can google it for more info)
The logic is "increase the count by 1 every second" - that's an interval. You don't want to use count as a dependency to achieve this behavior for two reasons: first, it's literally not a dependency when using the function version of setState, and second (and more importantly), count could change for reasons other than the interval increasing it. (If you wanted different logic based on count changing for other reasons, that would also be achievable, but it's a different requirement.) I'm not sure I understand your point about "the inner function taking longer than the interval". The inner function here is just a call to setState and is not asynchronous code, and it would be the same if it were in a timeout. You can absolutely use setInterval in React applications. There do exist libraries like useInterval which offer more functionality, but they ultimately are more sophisticated wrappers around setInterval. (And no criticism taken, I welcome the questions!)
React Strict mode simple replaces a "maybe" bug with a "certain" but which noob/blind devs might miss and therefore introduce bugs into the system. Strict mode is a big no-no for me, for years. It's catastrophic.