r/javascript • • Jan 15 '21

Svelte beats react for developer satisfaction in 2020

https://2020.stateofjs.com/en-US/technologies/front-end-frameworks/
346 Upvotes

231 comments sorted by

View all comments

Show parent comments

5

u/andrei9669 Jan 16 '21

I mean, if you have solid eslint rules, and you know basics, it's hard to fuck up.

-1

u/qudat Jan 16 '21

There are eslint rules that prevent infinite loops in your application?

-1

u/andrei9669 Jan 16 '21

No, for that you need 2 brain cells in your skull. What I mean is that there are hook eslint rules that help out eith basic stuff.

3

u/qudat Jan 16 '21

Yikes, I’m glad I’m not on your team with that kind of attitude. Good luck!

-1

u/andrei9669 Jan 16 '21

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.

1

u/frankandsteinatlaw Jan 16 '21

When apps get complicated enough this can be tricky. It’s just a layer that doesn’t feel necessary tbh.

1

u/andrei9669 Jan 16 '21

what do you mean? what doesn't seem necessary?

1

u/frankandsteinatlaw Jan 16 '21

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.

1

u/andrei9669 Jan 16 '21

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:

const useCustomHook = (condition, value) => {
    const [state, setState] = useState([]);

    useEffect(()=>{
        if(condition) {
            setState(() => [...state, value] )
        }
    }, [state, value, condition]);

    return state;
}

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:

const useCustomHook = (condition, value) => {
    const [state, setState] = useState([]);

    useEffect(()=>{
        if(condition) {
            setState((prevState) => [...prevState, value] )
        }
    }, [value, condition]);

    return state;
}

1

u/frankandsteinatlaw Jan 16 '21

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.

1

u/andrei9669 Jan 16 '21

I kinda get it but I also don't. Would be really nice to see a practical example.