Reactjs 如何在cleaup函数中的React js中执行fileReader.removeEventListener

Reactjs 如何在cleaup函数中的React js中执行fileReader.removeEventListener,reactjs,react-hooks,use-effect,use-ref,Reactjs,React Hooks,Use Effect,Use Ref,我在React js中实现了一个图像预览器 这是你的电话号码 . 当组件卸载时,我想移除VentListener,但我无法这样做。 我试着用useRef做这个,但是得到了未定义的错误和这个警告 The ref value 'fileReader.current' will likely have changed by the time this effect cleanup function runs. If this ref points to a node rendered by React

我在React js中实现了一个图像预览器 这是你的电话号码 . 当组件卸载时,我想移除VentListener,但我无法这样做。 我试着用useRef做这个,但是得到了未定义的错误和这个警告

The ref value 'fileReader.current' will likely have changed by the time this effect cleanup function runs. If this ref points to a node rendered by React, copy 'fileReader.current' to a variable inside the effect, and use that variable in the cleanup function.

您可以在useffect中返回一个cleanup函数,这是处理该函数的推荐方法。请参阅:

这不起作用,因为我已经试过了。它不起作用,因为我们将事件侦听器添加到的对象不同。此外,如果您确定提供的答案,您可以编辑codesandbox并共享链接。