what's wrong with my attitude? maybe I was a bit harsh, but expecting common sense isn't as high of a bar to reach. Sure, if you are new to programming you might make a stupid mistake or two, maybe more. not saying I didn't do any myself.
Sorry, I wasn’t clear. I mean that there are ways to cause infinite loops in React centered around dependency arrays that don’t feel obvious as the logic trail can move up and down the tree. In some cases the rules of hooks actually make this happen if you rely on the dev array as a “run this code when these values update” situation. To be safer you basically need to ignore the deep array, let it get filled by the lint rule, and then code careful logic inside your hook to ensure that no matter how many times the hook is run you don’t end up calling extra logic. Or you use refs if you think you can ignore function reference updates (which is dangerous in the other direction)
In that world, useEffect is more annoying to work with, with more bloat than originally promised.
When I say there’s a layer that feels unnecessary I’m mostly talking about dependency arrays I guess.
can I ask, do you even know the purpose of the dependency array? and if you feel it is unnecessary, how would you replace its functionality?
also, the more I use it, the less I run into these issues. Could you bring a specific example of the most common issue you run into, that would cause inf loop?
cus only reason that comes to my mind would be something like this:
but this is obviously the wrong way of doing things. I have seen developers, who don't have much experience with useEffect, do stuff like this, so this isn't something new. Just in case, I'm gonna add the correct way of doing this:
Thank you for the detailed response, though it wasn’t exactly what I meant. My main issue with the dep array (especially in the useEffect case) is that it was billed as one thing but became two. In the original announcement of hooks it was “use this array to control when code is executed”. But in reality it can be that, but is also, “any reference not in this array is a stale reference, use the dep array to tell react what shouldn’t be stale”.
So you may have a hook working just fine, but then you take a function prop and need to call it inside your effect function. Okay. So now I need to handle this. Do I just throw it in there (and not the dep array) and risk staleness? Do I put it in the dep array and risk a parent component not properly memoizing it, creating potential looping but always unneeded execution? Do I guard against that inside this component by internally memoizing it, adding bloat to the way we write code all over the app?
I’m not saying “React should fix these issues in this really obvious way” but what I am saying is, “this doesn’t feel great - why do I need to think about all this just to use a new function in here?”
I think this should be simpler. Maybe it could be simpler inside Reacts paradigm, maybe not, but I know it can be simpler in general.
5
u/andrei9669 Jan 16 '21
I mean, if you have solid eslint rules, and you know basics, it's hard to fuck up.