POST
- Get current content from the editor state using
editorState.getCurrentContent()
and save that to a variable. - Convert the current state to a string with these methods:
JSON.stringify(convertToRaw(data));
GET
- Display the data in a Draft.js Editor set to
readOnly
- Fetch the post from the server and then convert it to readable content with this.
const contentState = convertFromRaw(JSON.parse(el.content)); const editorState = EditorState.createWithContent(contentState);
- Show it in an Editor like so:
<Editor editorState={editorState} readOnly={true}
GET(React Hooks)
useEffect(() => {
postId && getPost(postId)
}, [postId]);
const [editorState, setEditorState] = useState(EditorState.createEmpty())
useEffect(() => {
if(postcontent) {
const contentState = convertFromRaw(JSON.parse(postcontent));
const newEditorState = EditorState.createWithContent(contentState, decorator);
setEditorState(newEditorState);
} else {
console.log("false")
}
}, [postcontent])