0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Send Draft.js data to a server and fetch it with React hooks

Last updated at Posted at 2022-12-10

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])

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?