之前在 reactjs/rfcs 看到的 useEvent 就是用来解决这种问题的
```
function useEvent(handler) {
const handlerRef = useRef(null);
useLayoutEffect(() => {
handlerRef.current = handler;
});
return useCallback((...args) => {
const fn = handlerRef.current;
return fn(...args);
}, []);
}
```
或者直接
```
const xxx = useRef();
xxx = () => {};
...
xxx.current();
``` |