Skip to content

QuizNot attempted

useSyncExternalStore third argument

What is the purpose of the third argument that could optionally be passed to useSyncExternalStore (not shown here)?
function useWindowWidth() {
return useSyncExternalStore(
(callback) => {
window.addEventListener('resize', callback);
return () => window.removeEventListener('resize', callback);
},
() => window.innerWidth
);
}
function App() {
const width = useWindowWidth();
return <div>{width}</div>;
}
1–4 to choose, Enter to submit
Show answer & explanation

Correct answer:

  • It provides a fallback value to use during server-side rendering / hydration (getServerSnapshot)
  • The optional third argument is getServerSnapshot: it supplies the value during server rendering and hydration, where window doesn't exist, and must match on server and client.
  • Not a debounce delay: the store decides when to notify.
  • Not an equality comparator: React compares snapshots with Object.is (selectors live in the useSyncExternalStoreWithSelector shim).
  • Not 'only two arguments': the third exists for SSR.