Skip to content

Instantly share code, notes, and snippets.

@pranshuagrawal
Created July 1, 2022 19:54
Show Gist options
  • Save pranshuagrawal/1f5d72063670e5eae1648508dfd4b7e2 to your computer and use it in GitHub Desktop.
Save pranshuagrawal/1f5d72063670e5eae1648508dfd4b7e2 to your computer and use it in GitHub Desktop.
const EditorComponent = ({details, setDetails}) => {
const [editorState, setEditorState] = React.useState(
EditorState.createEmpty()
);
React.useEffect(() => {
setEditorState(
details.editorContent
? EditorState.createWithContent(
convertFromRaw(details.editorContent)
)
: EditorState.createEmpty()
); // This is doing the magic
}, []);
const onEditorStateChange = (editorValue) => {
const editorStateInHtml = draftToHtml(
convertToRaw(editorValue.getCurrentContent())
); // This is your HTML
setEditorState(editorValue); // Set your local state
setDetails((details) => ({
...details,
editorContent: convertToRaw(editorValue.getCurrentContent()),
})); // Use this to set your state in DB or any other storage
};
return (
<>
<Editor
toolbarHidden={false}
editorState={editorState}
onEditorStateChange={onEditorStateChange}
/>
</>
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment