'react-drajs-wysiwyg initial value from api calls

newbs here.. I want to set my react-draftjs-wysiwyg initial value from database (api call) inside useEffect. This is my useEffect code:

const [journal,setJournal] = useState([])

useEffect(() => {
const getJournal = async () => {
  try {
    const res = await publicRequest.get(`journals/find/${journalId}`);
    setJournal(res.data);
    if (journal) {
      setEditorState(
        EditorState.createWithContent(
          convertFromRaw(JSON.parse(journal.desc))
        )
      );
    }
  } catch (err) {
    console.log(err);
  }
};
getJournal();
console.log(journalId); //I get the journal ID
console.log(editorState); // I get the editorState
}, [journalId]);

But my draft jsEditor will be empty and always showing -> SyntaxError: Unexpected token u in JSON at position 0 at JSON.parse () at getJournal (Journal.js:50:1) in console (maybe because useEffect need to wait from api call).

I try to put journal.desc as dependency, it's showing on my editor, but the syntaxError still there and my console log, spamming the journal.desc.

is there any solution for this?

thanks



Solution 1:[1]

Nevermind, I change the journal.desc into res.data.desc, and it works

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 dippas