我看了下面的视频
https://egghead.io/lessons/react-store-values-in-localstorage-with-the-react-useeffect-hook
他起初写道
const initialCount = Number(window.localStorage.getItem('count') || 0);
后来他将其更正到下面以进行优化
const initialCount = () => Number(window.localStorage.getItem('count') || 0);
但我不知道为什么要对其进行优化
让我先知道谢谢
对于,
const initialCount = Number(window.localStorage.getItem('count')|| 0);
一旦安装了组件,将执行此生产线,这将需要一些时间和处理能力。
const initialCount =()=> Number(window.localStorage.getItem('count')|| 0);
同时,仅声明以上语句,因为它是一个函数,并且仅在调用后才执行。这会影响性能。
当我们使用函数时,仅在调用它时才执行。