-1

" Wrap getListViewSetting in a function for useState ", what does this mean?

 const [isListView, setIsListView] = useState(
    getListViewSetting(location?.pathname?.replace('/', ''), true),
  );
Alaa Khalila
  • 169
  • 1
  • 11
  • Does this answer your question? [What is useState() in React?](https://stackoverflow.com/questions/53165945/what-is-usestate-in-react) – Joel Apr 15 '21 at 21:37

1 Answers1

1

Whoever (or whatever tool) said this probably meant for you to do:

const [isListView, setIsListView] = useState(
  () => getListViewSetting(location?.pathname?.replace('/', ''), true)
);

If you pass a function into useState, then react will call that function exactly once to determine the initial value. If getListViewSetting is expensive, that can be a performance improvement, because the code you had will need to call it on every render, even though the value is useless other than on the first render.

Nicholas Tower
  • 72,740
  • 7
  • 86
  • 98