Created
July 1, 2022 19:54
-
-
Save pranshuagrawal/1f5d72063670e5eae1648508dfd4b7e2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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