Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
v3.0.11 introduced a regression affecting Sanity Studios running on React 18. It did not impact users on React 19.
The issue was inputs using
<Autocomplete>
often crashing with this error:I made a throwaway deploy of our test studio on react 18, where this can be reproduced by interacting with the reference input:
Screen.Recording.2025-09-18.at.19.46.23.mov
I published a canary of the fix on this branch (
@sanity/[email protected]
) and updated the throwaway deployment:Screen.Recording.2025-09-18.at.19.47.48.mov
After going down the rabbit hole it turns out the crash is triggered by the
setInputElement
state call in<Autocomplete>
, though only on React 18 and not React 19.Given that 19 batches all state updates, while 18 only batches state updates it started making sense.
With the batching in 19 state update is scheduled and thus the recursion limit isn't hit, while in 18 it's sync and now it crashes.
By wrapping it in
startTransition
it opts-in to batching in 18, while behaving mostly the same in 19. The difference being that react might choose to delay rendering with thereferenceElement
set if other state updates happen that respond to user input and such. In this case that's fine, if the user is typing in a text input it's more important to update the text input, than render the popover right away.Alternatively we could use
import {unstable_batchedUpdates} from 'react-dom'
, but I decided against it since it's not only prefixedunstable
, it's also undocumented.What to review
Is there enough context in the code comments?
I've applied the same fix to
<TreeItem>
, though we don't use it in the Studio codebase apps that use it run a similar risk on React 18 as<Autocomplete>
.Testing
Existing test suite is enough to cover 19. I've manually tested 18, and there's a test deployment you can use to confirm:
@sanity/[email protected]
on React 18