Description
I'm very sorry that I'm using the issue tracker to ask this question, but I think others might ask the same question after reading reactwg/react-18#41 (I don't have commenting rights).
function handleInputChange(e) {
const input = e.target.value
setInputValue(input)
startTransition(() => {
setSearchQuery(input)
});
}
What happens if the user types "ab"? i.e.:
// pseudocode representing the first event handler triggered by keystroke "a"
setInputValue("a")
startTransition(() => setSearchQuery("a"))
// pseudocode representing the second event handler triggered by keystroke "b"
setInputValue("ab")
startTransition(() => setSearchQuery("ab"))
From my understanding setInputValue("a")
and setInputValue("ab")
will batch generating a single rerender, the callback () => setSearchQuery("a")
passed to the first startTransition
will be cancelled, and only the second callback () => setSearchQuery("ab")
passed to the second startTransition
will be executed. i.e.:
// pseudocode representing the final logic after keystrokes "a" and "b"
setInputValue("a") // will batch with `setInputValue("ab")`
startTransition(() => setSearchQuery("a")) // noop, this callback will be cancelled
setInputValue("ab")
startTransition(() => setSearchQuery("ab"))
Am I correct?
I think this question is important because if the first callback passed to startTransition
really gets cancelled and I try to do more work inside it, I should have in mind that the additional work will not happen.