React 中从 useEffect 里安全地移除全局事件监听:为什么你的回调总是旧值
在 React 项目里给 window 或 document 绑定键盘快捷键、滚动监听、点击外部关闭下拉菜单,是再常见不过的需求。很多人会写在 useEffect 里,然后加上 removeEventListener,结果发现要么移除不掉导致内存泄漏,要么回调里拿到的 state 永远是初始值。最常见的写法是直接在 effect 内部定义函数并引用,但依赖数组写空数组,闭包就把第一次渲染的变量锁死了。另一个坑是函数引用每次渲染都变,导致监听被反复解绑又绑定,性能白白损耗。这篇文章从一个实际的下拉菜单组件出发,聊清楚闭包捕获、依赖数组和清理函数之间的关系,再给出用 useRef 保存回调、用 AbortController 统一取消、以及 useSyncExternalStore 管理外部状态的几种落地写法,顺带说明严格模式下 effect 执行两次到底会不会造成真实的重复监听。 ## 一个总是关不掉的 Dropdown 先看一段代码,这是一个点击按钮展开、点击外部区域收起的下拉菜单: ```jsx function Dropdown({ children }) { const [open, setOpen] = useState(false); const ref = useRef(null); useEffect(() => { const onClick = (e) => { if (ref.current && !ref.current.contains(e.target)) { setOpen(false); } }; document.addEventListener('click', onClick); return () => { document.removeEventListener('click', onClick); }; }, []); return ( setOpen((v) => !v)}>菜单 {open && {children}} ); } ``` 这段代码通常能跑,因为 setOpen 在闭包里是稳定的。但把 setOpen(false) 换成 console.log(open) 或者依赖某个 props,问题就来了:onClick 里读到的 open 永远是第一次渲染时的 false。依赖数组写空并不代表“只执行一次且逻辑永远最新”,它只是让这个 effect 不再重新执行,闭包捕获的变量也就冻结在那一刻。 如果你把依赖数组改成 [open],监听会被正确重建,但每次 open 变化都解绑再绑定一次,键盘事件这种高频场景完全没必要。更麻烦的是如果把依赖写成 [ref.current],它根本不触发更新,因为 ref 对象本身引用不变。 ## 闭包捕获不是 bug,是预期行为 React 的函数组件每次渲染都会创建一整套新的变量和函数。useEffect 的依赖数组决定“什么时候重新执行这个 effect”,执行时它捕获的是当前这次渲染的作用域。所以只要 effect 不重新跑,它内部的函数就看不到后续渲染的值。 理解这一点后,处理监听回调的思路就清晰了:让 effect 负责绑定和解绑的时机,让回调本身始终指向最新的逻辑。最直接的手段是把回调存进 ref: ```jsx function useEventListener(type, handler, target = window) { const handlerRef = useRef(handler); // 每次渲染后同步最新回调,不在渲染期间写 ref useEffect(() => { handlerRef.current = handler; }); useEffect(() => { const listener = (event) => handlerRef.current(event); target.addEventListener(type, listener); return () => target.removeEventListener(type, listener); }, [type, target]); } ``` 关键点有两个:一是更新 ref 的 effect 没有依赖数组,所以每次渲染后都跑,保证 ref 里是最新函数;二是绑定监听的 effect 不依赖 handler,所以不会因为回调变化而反复解绑。这个封装可以用在键盘事件、resize、scroll 等任意原生事件上。 ## 别忽略 removeEventListener 的引用要求 removeEventListener 必须传入和 addEventListener 完全相同的函数引用,否则移除无效。下面这种写法是错误的: ```js useEffect(() => { window.addEventListener('resize', () => setWidth(window.innerWidth)); return () => { // 这里的匿名函数是另一个引用,移除失败 window.removeEventListener('resize', () => setWidth(window.innerWidth)); }; }, []); ``` 每次渲染或者严格模式下 effect 执行两次时,就会堆积多个监听。正确做法是把函数存成变量,清理时引用同一个变量;或者干脆用 AbortController: ```jsx useEffect(() => { const controller = new AbortController(); window.addEventListener('resize', onResize, { signal: controller.signal }); return () => controller.abort(); }, []); ``` AbortController 在移除时不需要保留原函数引用,写起来更省心,现代浏览器支持也很完整。多个监听可以共用一个 controller,在组件卸载时一次性全部取消。 ## 严格模式下 effect 跑两次要不要慌 React 18 在开发模式的 StrictMode 下会故意挂载、卸载、再挂载组件,effect 因此执行两遍。这不是 bug,而是逼你暴露清理不彻底的问题。如果你的清理函数正确,第二次挂载前监听完好移除,最终只会剩一个。如果你写了上面的匿名函数那种清理,就会在 window 上留下两个监听,控制台里也能观察到日志打印两次。 所以看到 effect 跑两次,先检查清理逻辑,而不是急着关掉 StrictMode。真实生产构建不会重复挂载,但清理写错在正常的路由切换、条件渲染里同样会泄漏。 ## 用 useSyncExternalStore 管外部事件源 当监听产生的是组件要渲染的状态时,比如窗口宽度、网络在线状态,可以考虑 useSyncExternalStore。它把“订阅外部数据”这件事抽象出来,订阅函数和 getSnapshot 分开,避免手写 effect 里常见的快照不一致问题: ```jsx function useOnline() { const subscribe = (callback) => { window.addEventListener('online', callback); window.addEventListener('offline', callback); return () => { window.removeEventListener('online', callback); window.removeEventListener('offline', callback); }; }; const getSnapshot = () => navigator.onLine; return useSyncExternalStore(subscribe, getSnapshot, () => true); } ``` subscribe 在组件生命周期里只订阅一次,React 内部保证在合适的时机调用 getSnapshot 拿到最新值并触发重渲染。这种写法比“在 effect 里 setState”更可靠,尤其能避免初次渲染时快照滞后一帧的问题。 ## 写在哪一层更合适 如果事件只影响一个小组件,比如下拉菜单的点击外部关闭,把监听放在组件内部完全没问题。但如果是全局快捷键、滚动进度、页面可见性这类跨组件的状态,建议抽成自定义 Hook 或者放进一个轻量的 store,避免每个组件都去 addEventListener。同时记得给监听加 passive: true(滚动、触摸事件)或者改用 pointer 事件统一鼠标和触摸,减少主线程阻塞。 监听本身不复杂,难的是把清理和闭包这两件事同时处理好。什么时候该重建监听、什么时候该用 ref 绕开依赖、什么时候该交给 useSyncExternalStore,判断清楚了,这类问题基本就不会再反复冒出来。 推荐查看天气:http://www.362tq.com/mudan/10tian.html 推荐查看天气:http://www.362tq.com/mudan/15tian.html 推荐查看天气:http://www.362tq.com/mudan/40tian.html 推荐查看天气:http://www.362tq.com/caoxian/ 推荐查看天气:http://www.362tq.com/caoxian/zuotian.html 推荐查看天气:http://www.362tq.com/caoxian/7tian.html 推荐查看天气:http://www.362tq.com/caoxian/10tian.html 推荐查看天气:http://www.362tq.com/caoxian/15tian.html 推荐查看天气:http://www.362tq.com/caoxian/40tian.html 推荐查看天气:http://www.362tq.com/danxian/ 推荐查看天气:http://www.362tq.com/danxian/zuotian.html 推荐查看天气:http://www.362tq.com/danxian/7tian.html 推荐查看天气:http://www.362tq.com/danxian/10tian.html 推荐查看天气:http://www.362tq.com/danxian/15tian.html 推荐查看天气:http://www.362tq.com/danxian/40tian.html 推荐查看天气:http://www.362tq.com/chengwu/ 推荐查看天气:http://www.362tq.com/chengwu/zuotian.html 推荐查看天气:http://www.362tq.com/chengwu/7tian.html 推荐查看天气:http://www.362tq.com/chengwu/10tian.html 推荐查看天气:http://www.362tq.com/chengwu/15tian.html 推荐查看天气:http://www.362tq.com/chengwu/40tian.html 推荐查看天气:http://www.362tq.com/juye/ 推荐查看天气:http://www.362tq.com/juye/zuotian.html 推荐查看天气:http://www.362tq.com/juye/7tian.html 推荐查看天气:http://www.362tq.com/juye/10tian.html 推荐查看天气:http://www.362tq.com/juye/15tian.html 推荐查看天气:http://www.362tq.com/juye/40tian.html 推荐查看天气:http://www.362tq.com/hezeyuncheng/ 推荐查看天气:http://www.362tq.com/hezeyuncheng/zuotian.html 推荐查看天气:http://www.362tq.com/hezeyuncheng/7tian.html 推荐查看天气:http://www.362tq.com/hezeyuncheng/10tian.html 推荐查看天气:http://www.362tq.com/hezeyuncheng/15tian.html 推荐查看天气:http://www.362tq.com/hezeyuncheng/40tian.html 推荐查看天气:http://www.362tq.com/juancheng/ 推荐查看天气:http://www.362tq.com/juancheng/zuotian.html 推荐查看天气:http://www.362tq.com/juancheng/7tian.html 推荐查看天气:http://www.362tq.com/juancheng/10tian.html 推荐查看天气:http://www.362tq.com/juancheng/15tian.html 推荐查看天气:http://www.362tq.com/juancheng/40tian.html 推荐查看天气:http://www.362tq.com/dingtao/ 推荐查看天气:http://www.362tq.com/dingtao/zuotian.html 推荐查看天气:http://www.362tq.com/dingtao/7tian.html 推荐查看天气:http://www.362tq.com/dingtao/10tian.html 推荐查看天气:http://www.362tq.com/dingtao/15tian.html 推荐查看天气:http://www.362tq.com/dingtao/40tian.html 推荐查看天气:http://www.362tq.com/dongming/ 推荐查看天气:http://www.362tq.com/dongming/zuotian.html 推荐查看天气:http://www.362tq.com/dongming/7tian.html 推荐查看天气:http://www.362tq.com/dongming/10tian.html 推荐查看天气:http://www.362tq.com/dongming/15tian.html 推荐查看天气:http://www.362tq.com/dongming/40tian.html 推荐查看天气:http://www.362tq.com/henan/ 推荐查看天气:http://www.362tq.com/henan/zuotian.html 推荐查看天气:http://www.362tq.com/henan/7tian.html 推荐查看天气:http://www.362tq.com/henan/10tian.html 推荐查看天气:http://www.362tq.com/henan/15tian.html 推荐查看天气:http://www.362tq.com/henan/40tian.html 推荐查看天气:http://www.362tq.com/zhengzhou/ 推荐查看天气:http://www.362tq.com/zhengzhou/zuotian.html 推荐查看天气:http://www.362tq.com/zhengzhou/7tian.html 推荐查看天气:http://www.362tq.com/zhengzhou/10tian.html 推荐查看天气:http://www.362tq.com/zhengzhou/15tian.html 推荐查看天气:http://www.362tq.com/zhengzhou/40tian.html 推荐查看天气:http://www.362tq.com/zhongyuan/ 推荐查看天气:http://www.362tq.com/zhongyuan/zuotian.html 推荐查看天气:http://www.362tq.com/zhongyuan/7tian.html 推荐查看天气:http://www.362tq.com/zhongyuan/10tian.html 推荐查看天气:http://www.362tq.com/zhongyuan/15tian.html 推荐查看天气:http://www.362tq.com/zhongyuan/40tian.html 推荐查看天气:http://www.362tq.com/erqi/ 推荐查看天气:http://www.362tq.com/erqi/zuotian.html 推荐查看天气:http://www.362tq.com/erqi/7tian.html 推荐查看天气:http://www.362tq.com/erqi/10tian.html 推荐查看天气:http://www.362tq.com/erqi/15tian.html 推荐查看天气:http://www.362tq.com/erqi/40tian.html 推荐查看天气:http://www.362tq.com/guancheng/ 推荐查看天气:http://www.362tq.com/guancheng/zuotian.html 推荐查看天气:http://www.362tq.com/guancheng/7tian.html 推荐查看天气:http://www.362tq.com/guancheng/10tian.html 推荐查看天气:http://www.362tq.com/guancheng/15tian.html 推荐查看天气:http://www.362tq.com/guancheng/40tian.html 推荐查看天气:http://www.362tq.com/jinshui/ 推荐查看天气:http://www.362tq.com/jinshui/zuotian.html 推荐查看天气:http://www.362tq.com/jinshui/7tian.html 推荐查看天气:http://www.362tq.com/jinshui/10tian.html 推荐查看天气:http://www.362tq.com/jinshui/15tian.html 推荐查看天气:http://www.362tq.com/jinshui/40tian.html 推荐查看天气:http://www.362tq.com/shangjie/ 推荐查看天气:http://www.362tq.com/shangjie/zuotian.html 推荐查看天气:http://www.362tq.com/shangjie/7tian.html 推荐查看天气:http://www.362tq.com/shangjie/10tian.html 推荐查看天气:http://www.362tq.com/shangjie/15tian.html 推荐查看天气:http://www.362tq.com/shangjie/40tian.html 推荐查看天气:http://www.362tq.com/zhongmou/ 推荐查看天气:http://www.362tq.com/zhongmou/zuotian.html 推荐查看天气:http://www.362tq.com/zhongmou/7tian.html 推荐查看天气:http://www.362tq.com/zhongmou/10tian.html 推荐查看天气:http://www.362tq.com/zhongmou/15tian.html 推荐查看天气:http://www.362tq.com/zhongmou/40tian.html 推荐查看天气:http://www.362tq.com/gongyi/ 推荐查看天气:http://www.362tq.com/gongyi/zuotian.html 推荐查看天气:http://www.362tq.com/gongyi/7tian.html 推荐查看天气:http://www.362tq.com/gongyi/10tian.html 推荐查看天气:http://www.362tq.com/gongyi/15tian.html 推荐查看天气:http://www.362tq.com/gongyi/40tian.html 推荐查看天气:http://www.362tq.com/xingyang/ 推荐查看天气:http://www.362tq.com/xingyang/zuotian.html 推荐查看天气:http://www.362tq.com/xingyang/7tian.html 推荐查看天气:http://www.362tq.com/xingyang/10tian.html 推荐查看天气:http://www.362tq.com/xingyang/15tian.html 推荐查看天气:http://www.362tq.com/xingyang/40tian.html 推荐查看天气:http://www.362tq.com/xinmi/ 推荐查看天气:http://www.362tq.com/xinmi/zuotian.html 推荐查看天气:http://www.362tq.com/xinmi/7tian.html 推荐查看天气:http://www.362tq.com/xinmi/10tian.html 推荐查看天气:http://www.362tq.com/xinmi/15tian.html 推荐查看天气:http://www.362tq.com/xinmi/40tian.html 推荐查看天气:http://www.362tq.com/xinzheng/ 推荐查看天气:http://www.362tq.com/xinzheng/zuotian.html 推荐查看天气:http://www.362tq.com/xinzheng/7tian.html 推荐查看天气:http://www.362tq.com/xinzheng/10tian.html 推荐查看天气:http://www.362tq.com/xinzheng/15tian.html 推荐查看天气:http://www.362tq.com/xinzheng/40tian.html 推荐查看天气:http://www.362tq.com/dengfeng/ 推荐查看天气:http://www.362tq.com/dengfeng/zuotian.html 推荐查看天气:http://www.362tq.com/dengfeng/7tian.html 推荐查看天气:http://www.362tq.com/dengfeng/10tian.html 推荐查看天气:http://www.362tq.com/dengfeng/15tian.html 推荐查看天气:http://www.362tq.com/dengfeng/40tian.html 推荐查看天气:http://www.362tq.com/kaifeng/ 推荐查看天气:http://www.362tq.com/kaifeng/zuotian.html 推荐查看天气:http://www.362tq.com/kaifeng/7tian.html 推荐查看天气:http://www.362tq.com/kaifeng/10tian.html 推荐查看天气:http://www.362tq.com/kaifeng/15tian.html 推荐查看天气:http://www.362tq.com/kaifeng/40tian.html 推荐查看天气:http://www.362tq.com/longting/ 推荐查看天气:http://www.362tq.com/longting/zuotian.html 推荐查看天气:http://www.362tq.com/longting/7tian.html 推荐查看天气:http://www.362tq.com/longting/10tian.html 推荐查看天气:http://www.362tq.com/longting/15tian.html 推荐查看天气:http://www.362tq.com/longting/40tian.html 推荐查看天气:http://www.362tq.com/shunhe/ 推荐查看天气:http://www.362tq.com/shunhe/zuotian.html 推荐查看天气:http://www.362tq.com/shunhe/7tian.html 推荐查看天气:http://www.362tq.com/shunhe/10tian.html 推荐查看天气:http://www.362tq.com/shunhe/15tian.html 推荐查看天气:http://www.362tq.com/shunhe/40tian.html 推荐查看天气:http://www.362tq.com/gulou/ 推荐查看天气:http://www.362tq.com/gulou/zuotian.html 推荐查看天气:http://www.362tq.com/gulou/7tian.html 推荐查看天气:http://www.362tq.com/gulou/10tian.html 推荐查看天气:http://www.362tq.com/gulou/15tian.html 推荐查看天气:http://www.362tq.com/gulou/40tian.html 推荐查看天气:http://www.362tq.com/yuwangtai/ 推荐查看天气:http://www.362tq.com/yuwangtai/zuotian.html 推荐查看天气:http://www.362tq.com/yuwangtai/7tian.html 推荐查看天气:http://www.362tq.com/yuwangtai/10tian.html 推荐查看天气:http://www.362tq.com/yuwangtai/15tian.html 推荐查看天气:http://www.362tq.com/yuwangtai/40tian.html 推荐查看天气:http://www.362tq.com/kaifengqixian/ 推荐查看天气:http://www.362tq.com/kaifengqixian/zuotian.html 推荐查看天气:http://www.362tq.com/kaifengqixian/7tian.html 推荐查看天气:http://www.362tq.com/kaifengqixian/10tian.html 推荐查看天气:http://www.362tq.com/kaifengqixian/15tian.html 推荐查看天气:http://www.362tq.com/kaifengqixian/40tian.html 推荐查看天气:http://www.362tq.com/tongxu/ 推荐查看天气:http://www.362tq.com/tongxu/zuotian.html 推荐查看天气:http://www.362tq.com/tongxu/7tian.html 推荐查看天气:http://www.362tq.com/tongxu/10tian.html 推荐查看天气:http://www.362tq.com/tongxu/15tian.html 推荐查看天气:http://www.362tq.com/tongxu/40tian.html 推荐查看天气:http://www.362tq.com/weishi/ 推荐查看天气:http://www.362tq.com/weishi/zuotian.html 推荐查看天气:http://www.362tq.com/weishi/7tian.html 推荐查看天气:http://www.362tq.com/weishi/10tian.html 推荐查看天气:http://www.362tq.com/weishi/15tian.html 推荐查看天气:http://www.362tq.com/weishi/40tian.html 推荐查看天气:http://www.362tq.com/lankao/ 推荐查看天气:http://www.362tq.com/lankao/zuotian.html 推荐查看天气:http://www.362tq.com/lankao/7tian.html 推荐查看天气:http://www.362tq.com/lankao/10tian.html 推荐查看天气:http://www.362tq.com/lankao/15tian.html 推荐查看天气:http://www.362tq.com/lankao/40tian.html 推荐查看天气:http://www.362tq.com/luoyang/ 推荐查看天气:http://www.362tq.com/luoyang/zuotian.html 推荐查看天气:http://www.362tq.com/luoyang/7tian.html 推荐查看天气:http://www.362tq.com/luoyang/10tian.html 推荐查看天气:http://www.362tq.com/luoyang/15tian.html 推荐查看天气:http://www.362tq.com/luoyang/40tian.html 推荐查看天气:http://www.362tq.com/laocheng/ 推荐查看天气:http://www.362tq.com/laocheng/zuotian.html 推荐查看天气:http://www.362tq.com/laocheng/7tian.html 推荐查看天气:http://www.362tq.com/laocheng/10tian.html 推荐查看天气:http://www.362tq.com/laocheng/15tian.html 推荐查看天气:http://www.362tq.com/laocheng/40tian.html 推荐查看天气:http://www.362tq.com/xigong/ 推荐查看天气:http://www.362tq.com/xigong/zuotian.html 推荐查看天气:http://www.362tq.com/xigong/7tian.html 推荐查看天气:http://www.362tq.com/xigong/10tian.html 推荐查看天气:http://www.362tq.com/xigong/15tian.html