" Wrap getListViewSetting
in a function for useState ", what does this mean?
const [isListView, setIsListView] = useState(
getListViewSetting(location?.pathname?.replace('/', ''), true),
);
" Wrap getListViewSetting
in a function for useState ", what does this mean?
const [isListView, setIsListView] = useState(
getListViewSetting(location?.pathname?.replace('/', ''), true),
);
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.