{"record":{"id":"f5aaf48ba015915e","repo":"facebook/react","slug":"cannot-update-optimistic-state-while-rendering-f5aaf4","errorCode":null,"errorMessage":"Cannot update optimistic state while rendering.","messagePattern":"Cannot update optimistic state while rendering\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/react-server/src/ReactFizzHooks.js","lineNumber":659,"sourceCode":"function unsupportedStartTransition() {\n  throw new Error('startTransition cannot be called during server rendering.');\n}\n\nfunction useTransition(): [\n  boolean,\n  (callback: () => void, options?: StartTransitionOptions) => void,\n] {\n  resolveCurrentlyRenderingComponent();\n  return [false, unsupportedStartTransition];\n}\n\nfunction useHostTransitionStatus(): TransitionStatus {\n  resolveCurrentlyRenderingComponent();\n  return NotPendingTransition;\n}\n\nfunction unsupportedSetOptimisticState() {\n  throw new Error('Cannot update optimistic state while rendering.');\n}\n\nfunction useOptimistic<S, A>(\n  passthrough: S,\n  reducer: ?(S, A) => S,\n): [S, (A) => void] {\n  resolveCurrentlyRenderingComponent();\n  return [passthrough, unsupportedSetOptimisticState];\n}\n\nfunction createPostbackActionStateKey(\n  permalink: string | void,\n  componentKeyPath: KeyNode | null,\n  hookIndex: number,\n): string {\n  if (permalink !== undefined) {\n    // Don't bother to hash a permalink-based key since it's already short.\n    return 'p' + permalink;","sourceCodeStart":641,"sourceCodeEnd":677,"githubUrl":"https://github.com/facebook/react/blob/eafeac097ba51e1eab809c07102126bd5f8e5425/packages/react-server/src/ReactFizzHooks.js#L641-L677","documentation":"The SSR renderer (Fizz) implements useOptimistic as a stub: during server rendering it returns the passthrough value unchanged plus a dispatch function (unsupportedSetOptimisticState) that always throws. The server keeps no update queue, so any attempt to apply an optimistic update while rendering on the server fails immediately with this error.","triggerScenarios":"Invoking the second element returned by useOptimistic() while a component renders on the server: dispatching during render (derived-state pattern), calling the setter from a helper that the SSR pass executes, or calling it inside logic shared between client and server that runs during SSR.","commonSituations":"Components shared between client and SSR that call the optimistic setter during render; React 19 useOptimistic adoption in code paths that also execute on the server; dispatching at module scope or in top-level code the server evaluates.","solutions":["Call the optimistic setter only from client-side event handlers, form actions, or transition callbacks","If you need a derived initial value during render, compute it and pass it as the passthrough argument instead of dispatching","Move the interactive logic into a component marked 'use client' so it never executes during SSR"],"exampleFix":"// before\nfunction Todo({todo}) {\n  const [optimistic, addOptimistic] = useOptimistic(todo, reducer);\n  addOptimistic({done: true}); // dispatch during render -> throws during SSR\n  return <li>{optimistic.text}</li>;\n}\n\n// after\nfunction Todo({todo}) {\n  const [optimistic, addOptimistic] = useOptimistic(todo, reducer);\n  // dispatch only from an event handler (runs on the client after hydration)\n  return <button onClick={() => addOptimistic({done: true})}>{optimistic.text}</button>;\n}","handlingStrategy":"type-guard","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":["Treat render as pure: never call a hook dispatcher during render","Keep useOptimistic dispatches inside event handlers, transitions, or form actions","Review shared components for render-phase dispatches (derived-state anti-pattern) before running them under SSR"],"tags":["ssr","use-optimistic","hooks","server-rendering"],"backgroundTag":"hooks-unsupported-in-server-rendering","analyzedSha":"eafeac097ba51e1eab809c07102126bd5f8e5425","analyzedAt":"2026-08-21T22:01:08.818Z","schemaVersion":2},"datasetVersion":"2026-08-21T23:17:16.201Z"}