Can't run hooks in OnChangePlugin - autosave feature #3355
wickandpiper
started this conversation in
Show and tell
Replies: 2 comments 1 reply
-
Figured out a workaround for this in case it helps others. I changed the approach to creating a plugin that saves based on when someone stops typing, instead of trying to dovetail with onChangePlugin. I also do a equals check to see if the content is different from the current content. AutoSave plugin below:
|
Beta Was this translation helpful? Give feedback.
0 replies
-
Your issue is export function AutosavePlugin((): AutosavePluginProps) {
const dispatch = useDispatch()
const onChange = useDebounceCallback(
(editorState: EditorState) => {
// do your thing
dispatch({})
},
1000,
false
);
return <OnChangePlugin onChange={onChange} ignoreSelectionChange />;
}
export function MyEditor() {
return (
<LexicalComposer>
<Editor />
<AutosavePlugin />
</LexicalComposer>
)
} |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I'm trying to implement an auto-save feature in a Lexical React project. The logic is as follows: 1. user stops typing > 2. onChange handler will send a debounced Redux dispatch method which updates to my database with the latest editor state/content (code not illustrated below).
However, it appears this is not possible as I need to use a useDispatch Redux hook to accomplish this, and they aren't allowed with the LexicalOnChangePlugin. e.g. import
{OnChangePlugin} from '@lexical/react/LexicalOnChangePlugin'
Implemented in my Lexical editor as follows:
<OnChangePlugin onChange={MyOnChangePlugin} />
And MyOnChangePlugin looks like:
I assume I may need to register a new command or listener as part of a new plugin, but I essentially would be recreating the LexicalOnChangePlugin - so I assume I will be in a catch 22 situation.
Any help or advice on this? Thanks in advance.
Beta Was this translation helpful? Give feedback.
All reactions