r/reactjs • • Oct 31 '18

Why the hate for React Hooks?

I've been seeing a lot of mixed feelings about React Hooks since it was announced and can't quite figure out why. Sure it's a deviation from the norm, but completely optional and certainly lowers the barrier to entry for new developers.

Can someone explain why all the negative feelings are rising about it?

19 Upvotes

74 comments sorted by

View all comments

Show parent comment

1

u/strothjs Oct 31 '18

For your guard there, you'd need to add:

const guard = useMemo(() => [0, 1, 2], [value1, value2, value2]);

That should return the same reference for comparison's sake.

3

u/gaearon React core team Oct 31 '18

It would be easier to spread that array.

const myArray = [1, 2, 3]
useEffect(fn, [...myArray])

-1

u/leixiaotie Oct 31 '18

... This feels so wrong for me, but it works in most cases. How can I describe it.

It's because js can somehow compare object by it's reference, ex:

var a = [0, 1, 2]; var b = a; var c = [0, 1, 2];
console.log(a === b); //true
console.log(a === c); //false

Same with object

var a1 = { x: 1, y: 2 }; var b1 = a1; var c1 = { x: 1, y: 2 };
console.log(a1 === b1); // true
console.log(a1 === c1); // false

So both works with ===, but not because it can compare array by value, but by reference, which will not works in some cases.