Reputation: 119
Recently I came across some weird usage of useMemo hook:
const memo = useMemo(callback, false);
As a second argument, instead of dependency is passed false.
Is this a valid code? React documentation states that dependency should be an array. What is the purpose of using false?
Upvotes: 4
Views: 6186
Reputation: 13823
Is this a valid code?
It depends on what you mean by valid
.
Is it a valid call to React API? no.
While this code works today, passing false
as dependency list is not mentioned in the documentation and the behaviour can change in any future react release.
In summary: update your code to useMemo(callback, [])
.
Upvotes: 8
Reputation: 119
Actually I’ve analyzed react-reconsiler and it turnes out that above code is equivalent to this:
const memo = useMemo(callback, []);
That’s because of javascript quirks, where:
false.length = undefined;
false[1] = undefined;
So consequently:
undefined === undefined // true
Upvotes: 4