I have a question about useEffect's cleanup callback function. In one word, return cleanup gives an error, but return () => cleanup() works well. Yes, they are different, but how are they different? Does anyone know how to explain? Thanks,
I came across this question while I am writing with codemirror.
The error message I see is
Unhandled Runtime Error
TypeError: Cannot read properties of undefined (reading 'plugins')
My hooks
export const useCodeMirror = () => {
const state = ...
const parent = ...
useEffect(() => {
const view = new EditorView({state: state, parent: parent})
// return view.destroy raises an error
return () => view.destroy() // works perfectly
}, [element])
}
node_modules/@codemirror/view/dist/index.js L:6740-6750
destroy() {
for (let plugin of this.plugins)
plugin.destroy(this);
this.plugins = [];
this.inputState.destroy();
this.dom.remove();
this.observer.destroy();
if (this.measureScheduled > -1)
cancelAnimationFrame(this.measureScheduled);
this.destroyed = true;
}
package.json
{
"dependencies": {
...
"codemirror": "^6.0.1",
...
}
}