jodit / jodit-react

React wrapper for Jodit
MIT License
363 stars 120 forks source link

Cannot read properties of undefined (reading 'off') #191

Closed ludiguo closed 2 years ago

ludiguo commented 2 years ago

Is there an error in this installation package, or is it a problem that I haven't configured well in my project? It was good before, and suddenly reported an error

xdan commented 2 years ago

Can you show me stack trace?

vimutti77 commented 2 years ago

This is a reproducible example https://codesandbox.io/s/jodit-react-event-bug-on-unmount-forked-57mlv1

If you click on unmount jodit button, It will throw an error.

rafaelfndev commented 2 years ago

I just upgrade to version 1.3.5 to use onPaste method and now I get this error too.

Screenshot_41

rouftom commented 2 years ago

This error occurs because events related to the JoditEditor component are not remembered when react renders. For that, you can use react's useMemo to fix the issue as follows:

import * as React from 'react

const handleJoditOnBlur = React.useMemo((newContent) => { // Deal with the new content })

const handleJoditOnChange = React.useMemo((newContent) => { // Deal with the new content })

<JoditEditor config={config} value={value} onBlur={handleJoditOnBlur} onChange={handleJoditOnChange} />

haimbrum commented 2 years ago

Looks like the problem is caused as a result of a bad cleanup.

@xdan issue is introduce on this commit: https://github.com/jodit/jodit-react/commit/7bc5d7e9683ec786c861ddbb9118137df532e582 I'm not sure why but it appears that on the cleanup callback we don't have a reference to the events object anymore.

image

xdan commented 2 years ago

Fixed in 1.3.6