codex-team / codex-team/editor.js

Strange behavior when accessing state value in onChange

Open
#2,562 0 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
TypeScript
Stars
31.9k
Forks
2.2k
Avg merge
1d 1h
Merged PRs (30d)
1

Description

I get this strange behavior when I am trying to access a state value in `onChange` — it always return the original value that is set with `useState`

`Vite + React`

Please see the video here.

https://github.com/codex-team/editor.js/assets/1012916/bdbd470b-3944-4be2-a28b-044d5c541cad

Anyone have any idea what causes this?

Code
```js
import React, { useRef } from 'react'
import EditorJS from '@editorjs/editorjs';
function App() {

const ref = useRef(null);
const [title, setTitle] = React.useState("this is a title")
const [detail, setDetail] = React.useState({})

const onChange = async (api, event) => {
console.log("title is ", title)
const content = await ref.current.saver.save()
setDetail(content)
}

React.useEffect(() => {
if (!ref.current) {

const editor = new EditorJS({
holder: "editorjs",
onReady: () => {
console.log("onReady")
ref.current = editor
},
data: detail,
logLevel: "ERROR",
tools: {

},
onChange: onChange
});
}

}, [])

return (
<>
{ setTitle(e.target.value) }} />
{ alert(title) }}>Title is


{title}

)
}

export default App

```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.