Tengo un problema con la biblioteca react-editor-js. Estoy tratando de obtener datos de él, pero no se genera y cuando verifico el valor de referencia de instancia, es nulo. Aquí está el código.
const Create = () => { const instanceRef = React.useRef(null); let data = { '1': 'test' } async function handleSave() { const savedData = await instanceRef.current.save() console.log(savedData) } return ( <div> <Header Name='Создание'/> <Menu/> <div className="editor"> <ReactEditorJS instanceRef={(instance) => (instanceRef.current = instance)} tools={EDITOR_JS_TOOLS} data={data} onChange={() => handleSave} /> </div> </div> ); };si uso
onChage={() => handleSave}me sale este error
Uncaught (in promise) TypeError: Cannot read properties of null (reading 'save')¡Gracias por tu ayuda!
Según los documentos de ReactEditorJS , debe usar onInitialize para guardar su instancia al inicializar.
Entonces, en tu caso, prueba esto:
const Create = () => { const instanceRef = React.useRef(null); let data = { '1': 'test' } async function handleSave() { const savedData = await instanceRef.current.save() console.log(savedData) } return ( <div> <Header Name='Создание'/> <Menu/> <div className="editor"> <ReactEditorJS //changed instanceRef to onInitialize onInitialize={(instance) => (instanceRef.current = instance)} tools={EDITOR_JS_TOOLS} data={data} //need to add '()' to call handleSave // or you can change it to onChange={handleSave} onChange={() => handleSave()} /> </div> </div> ); };Referencias - ¿Cómo acceder a la instancia de editor-js?
Nota: no he probado esta solución, pero al leer la documentación he agregado esta respuesta, avíseme si no funciona.