Jungwoo-An / react-editor-js

⚛️📝The unofficial editor-js component for React
https://codesandbox.io/s/react-editor-js-v2-34bfl
MIT License
945 stars 77 forks source link

Is it possible to pass changing variables to `onChange` #184

Open lassegit opened 2 years ago

lassegit commented 2 years ago

I have an component that needs to store the editor data whenever it has changed. I am using the onChange function but besides the actual data which I can get via (const body = await api.saver.save(); or a ref as shown in the docs), I need to add a changing temporary id (changingTemporaryId in the example below) to the onChange function, but this value seem to be stale and doesn't change even if the surrounding value has. Is it possible to pass a changing variable to onChange or EditorJS?

Example:

function ReactComponent() {
  const changingTemporaryId = Math.random().toString().substring(2);

  const handleOnChange = async (api) => {
    const body = await api.saver.save();

    console.log(changingTemporaryId); // This value is stale and doesn't get updated with re-renders.

    fetch('/api/sections', {
      method: 'POST',
      body: JSON.stringify({body, changingTemporaryId}),
    });
  };

  return <Editor onChange={handleOnChange} />
}
proteye commented 2 years ago

I have the same problem. Please, fix it!

fortysev-en commented 2 years ago

Initialize your Editor as EditorJs and your onChange should work.

const EditorJS = createReactEditorJS(); and in the component pass onChange={handleSave}