最佳答案
我试图在函数组件中使用来自 lodash
的 throttle
方法,例如:
const App = () => {
const [value, setValue] = useState(0)
useEffect(throttle(() => console.log(value), 1000), [value])
return (
<button onClick={() => setValue(value + 1)}>{value}</button>
)
}
因为 useEffect
中的方法在每次渲染时都会重新声明,所以节流效果不起作用。
有人有简单的解决办法吗?