I have a problem with state synchronization. When I click on the outside of the editor (wanna close it) I want to pass the actual text back to parent node (callback function).
But when I click outside the queryText state seems to be always one step behind. (E.g.: If there is abc inside the Editor, I type d, CALLBACK: abc, I type e, CALLBACK: abcd and so on...).
How can I achieve that if I click outside of my Editor there will be the actual state of queryText?
import React, {useEffect, useState} from "react";
import AceEditor from "react-ace";
import "ace-builds/src-noconflict/mode-mysql";
import "ace-builds/src-noconflict/theme-eclipse";
import "./SQLEditor.css"
function SQLEditor({queryTab, active, callback}) {
const [queryText, setQueryText] = useState(queryTab[active].content)
//Setting a onClickListener somewhere else
//This function is getting called when I click outside of the Editor
function handleClickOutside() {
document.removeEventListener("mousedown", handleClickOutside);
console.log("CALLBACK:" + queryText) //Problem here
callback(active, {content: queryText})
setInEditor(false)
}
}
//Implementing useEffect for debugging
useEffect(() => {
console.log(queryText); //Here I'm getting the right one.
}
return (
<AceEditor
mode="mysql"
theme="eclipse"
name="blah2"
onChange={(newValue) => {
setQueryText(newValue) //Seting text to new value
console.log(newValue) //Getting the correct updated value
}}
fontSize={16}
/>
</div>
)
}
export default SQLEditor;