react-editor-js
react-editor-js copied to clipboard
Is it possible to pass changing variables to `onChange`
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} />
}
I have the same problem. Please, fix it!
Initialize your Editor as EditorJs and your onChange should work.
const EditorJS = createReactEditorJS(); and in the component pass onChange={handleSave}