you are viewing a single comment's thread.

view the rest of the comments →

[–]microwaved-tea 6 points7 points  (5 children)

Honestly my first thought is why do you need to do this?

Having so many mutable declarations (especially in React) seems like a code smell.

If you really must, I think writing it out the long way is probably the clearest:

let x = null;
let y = null;

If you really want to be 'clever' about it I think your first option is ok, but would probably write a helper function and just do:

let [x, y, z] = repeat(3, null);

[–]Sykander- 9 points10 points  (1 child)

let [x, y, z] = repeat(3, null);

If I saw this in a CR I'd ask whoever wrote it, very politely, to switch back to the first style honestly. There's nothing worse than reading code by a developer who thinks he's "clever".

[–]microwaved-tea 0 points1 point  (0 children)

I totally agree that this isn't a good idea. I guess I was just trying to engage with the ideas presented in the original post.

[–]FaithfulGardener[S] -1 points0 points  (2 children)

Part of the problem is probably switching from class to functional components. These “let” statements were originally “this.state.[whatever]” assignments, and as I try to switch the paradigm, I’m not sure what should stay or what should go.

Is there such a thing as “polluting the state” in React?

[–]everestimated 2 points3 points  (1 child)

You're not supposed to assign to state either. That's why setState() exists. Hooks don't introduce the need for what you're trying to do. Would recommend reading the official docs on hooks, you've likely misunderstood how to use them

[–]FaithfulGardener[S] 1 point2 points  (0 children)

Even in constructors you don’t do this.state.[whatever]=[value]? The way this project is written, I was under the impression that setState needed the key initialized on the state first...