Open 981377660LMT opened 1 year ago
这个警告是因为你在组件卸载后还在尝试更新组件的状态。这通常发生在异步操作(如网络请求、定时器等)完成后,但此时组件已经被卸载。
在你的代码中,useEffect 的清理函数可能没有正确地取消订阅或异步任务。你可以在 useEffect 的返回函数中取消所有订阅和异步任务。
useEffect
例如,你的代码可能看起来像这样:
React.useEffect(() => { filterEventEmitter.on("show-normal-filter", (targetPath) => { if (path.join(",") === targetPath.join(",")) { handleVisibleChange(true); } }); return () => { filterEventEmitter.off("show-normal-filter", () => {}); }; }, [filterConfigItem]);
在这段代码中,你在清理函数中调用了 filterEventEmitter.off("show-normal-filter", () => {});,但是这并没有真正取消订阅,因为你传递给 off 的函数和你传递给 on 的函数不是同一个。
filterEventEmitter.off("show-normal-filter", () => {});
off
on
你应该这样做:
React.useEffect(() => { const callback = (targetPath) => { if (path.join(",") === targetPath.join(",")) { handleVisibleChange(true); } }; filterEventEmitter.on("show-normal-filter", callback); return () => { filterEventEmitter.off("show-normal-filter", callback); }; }, [filterConfigItem]);
在这段代码中,callback 是一个常量,所以在清理函数中取消订阅时,你可以确保你取消的是正确的订阅。
callback
这个警告是因为你在组件卸载后还在尝试更新组件的状态。这通常发生在异步操作(如网络请求、定时器等)完成后,但此时组件已经被卸载。
在你的代码中,
useEffect
的清理函数可能没有正确地取消订阅或异步任务。你可以在useEffect
的返回函数中取消所有订阅和异步任务。例如,你的代码可能看起来像这样:
在这段代码中,你在清理函数中调用了
filterEventEmitter.off("show-normal-filter", () => {});
,但是这并没有真正取消订阅,因为你传递给off
的函数和你传递给on
的函数不是同一个。你应该这样做:
在这段代码中,
callback
是一个常量,所以在清理函数中取消订阅时,你可以确保你取消的是正确的订阅。