Subscribe to a Document and Re-Render with React When Updated
is probably a good place to start.
In the example the fetch ends in a console log but it probably makes the most sense for whatever you're rerendering to include the current state of a destructured useState hook and set it inside the same spot the console log is happening.
So if you did
const [ authorName, setAuthorName ] = useState('')const subscription = client.listen(query, params)
.subscribe(update => {
const comment = update.result
setAuthorName(comment.author)
})<h1>{authorName}</h1>That was just my thought on the pattern. It seems to be corroborated by this repo:
https://github.com/sanity-io/syncing-example/blob/main/src/List.jsx but full disclosure I haven't built anything with the listen endpoint yet.
Was this answer helpful?
Sanity – Build the way you think, not the way your CMS thinks
Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.