Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React hooks useMemo, false as dependency

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?

like image 581
bearsie Avatar asked Dec 13 '22 10:12

bearsie


1 Answers

Is this a valid code?

It depends on what you mean by valid.

  • syntactically correct: yes (javascript, typescript)
  • allowed by tools: no (eslint, flow, typescript)

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, []).

like image 52
x00 Avatar answered Dec 20 '22 20:12

x00