How can I know the events from onBeforeInput and onInput are related?
export const MyApp = () => {
const onBeforeInput = (eventBefore) => {
console.log(eventBefore)
}
const onInput = (event) => {
console.log(event)
const eventBefore = getEventBefore() // how can i get eventBefore here?
console.log(eventBefore)
}
return (
<textarea onBeforeInput={onBeforeInput} onInput={onInput}/>
);
}
How can I use the associated eventBefore
inside onInput
?
Comment: I am asking because I need to use selectionStart
from eventBefore
inside onInput
.
2
Answers
I have found that using
onKeyDown
is better then usingonBeforeInput
, sinceonBeforeInput
does not trigger on backspace.Store the
eventBefore
in a variable common to both functionsIf you create the variable in an environment that encloses both functions, they should share a single variable:
beforeInput
will finish beforeonInput
starts, as long asbeforeInput
is synchronousJavascript is single-threaded, so unless you are using promises, or async/await, etc, you can be sure that
beforeInput
will complete first.Once the
beforeinput
event has completed, the browser will process the user input and then fire the input event.