How to Fix the useOptimistic “outside a transition or action” Error in React
In React, An optimistic state update occurred outside a transition or action. appears when you call the update function returned by useOptimistic outside a transition. To fix it, call the update function inside the function you pass to startTransition, or inside the function you pass to a <form>'s action. This error commonly appears when you write an optimistic update, such as a like button or posting a comment, that shows the result before the server responds.
I deliberately triggered this error in a test environment and copied the message as shown. After fixing it, I confirmed that the error goes away and that the optimistic value stays on screen until the save finishes.
The test environment was Ubuntu 24.04 with Node.js 22.23.5. I used a project created with the react-ts template from npm create vite@latest (react and react-dom 19.3.1, @types/react 19.3.3, Vite 8.4.0, TypeScript 7.0.2), and wrapped the component in StrictMode. I opened the page in Chromium 158 driven by Playwright 1.63.0. I did not test Next.js or other frameworks, or combinations with Server Functions.
- 1. Calling it from a click handler causes the error
- 2. The display flashes once and then reverts
- 3. Wrap it in startTransition to fix it
- 4. If you call it inside a form's action function, you do not need the wrapper
- 5. Inside startTransition, a setTimeout callback still causes the error
- 6. The production build shows no error, but the display still reverts
Calling it from a click handler causes the error
I prepared a button that shows a like count. When clicked, it calls saveLike, which waits one second in place of a server save. While the save is pending, the code is meant to show the count plus one using useOptimistic.
// Stand-in for a server save. Waits 1 second, then returns the saved like count
export function saveLike(next: number): Promise<number> {
return new Promise((resolve) => setTimeout(() => resolve(next), 1000));
}
import { useOptimistic, useState } from 'react';
import { saveLike } from './api';
// Wrong: setOptimisticLikes is called directly in the click handler
export default function BadClick() {
const [likes, setLikes] = useState(0);
const [optimisticLikes, setOptimisticLikes] = useOptimistic(likes);
async function handleClick() {
setOptimisticLikes(likes + 1);
const saved = await saveLike(likes + 1);
setLikes(saved);
}
return <button type="button" onClick={handleClick}>いいね {optimisticLikes}</button>;
}
Neither the type check (npx tsc -p tsconfig.app.json --noEmit) nor oxlint, which is included in the template, reported any errors or warnings. I opened the page on the development server started by npm run dev (which runs vite) and clicked the button. The DevTools console showed the following message as a red console.error line.
An optimistic state update occurred outside a transition or action. To fix, move the update to an action, or wrap with startTransition.
The message means: "An optimistic state update occurred outside a transition or action. To fix it, move the update into an action, or wrap it with startTransition."

The error explanation in the useOptimistic reference capitalizes "Transition" and "Action." The message actually printed by react-dom 19.3.1 was the lowercase transition and action shown above.
The display flashes once and then reverts
The error is not the only symptom. I recorded how many milliseconds after the click the button text changed, and got the following.
0ms "いいね 0" → 5ms "いいね 1" → 6ms "いいね 0" → 1004ms "いいね 1"
Right after the button shows 1 like, it goes back to 0 likes and stays there until the save finishes one second later. Visually, the button does nothing for a full second after you press it, which defeats the purpose of an optimistic update.
The same error explanation in the reference also states that calling the update outside an action shows the optimistic value only briefly and then reverts to the original value. This is because there is no transition to keep the optimistic value in place until the save completes.
Wrap it in startTransition to fix it
I moved the update function call into the function passed to startTransition.
import { startTransition, useOptimistic, useState } from 'react';
import { saveLike } from './api';
// Fixed: setOptimisticLikes is called inside the function passed to startTransition
export default function GoodTransition() {
const [likes, setLikes] = useState(0);
const [optimisticLikes, setOptimisticLikes] = useOptimistic(likes);
function handleClick() {
startTransition(async () => {
setOptimisticLikes(likes + 1);
const saved = await saveLike(likes + 1);
startTransition(() => {
setLikes(saved);
});
});
}
return <button type="button" onClick={handleClick}>いいね {optimisticLikes}</button>;
}
Clicking the button produced no error. The only thing in the console was the React DevTools info message.

The button text changed to show 1 like right after the click and did not change again.
0ms "いいね 0" → 4ms "いいね 1"
The optimistic value (1 like) stays on screen until the save finishes. When the save finishes, the real likes value becomes 1, so the display continues to show 1 like.
I also wrapped the setLikes call after await in its own startTransition. The caveats section of the startTransition reference says that state updates after an await are not treated as a transition unless they are wrapped in a separate startTransition. I used the startTransition function imported as import { startTransition } from 'react' as is.
If you call it inside a form's action function, you do not need the wrapper
If the button is the submit button of a <form>, you can also call the update function inside the function passed to the form's action.
import { startTransition, useOptimistic, useState } from 'react';
import { saveLike } from './api';
// Fixed: called inside the function passed to the form's action
export default function GoodFormAction() {
const [likes, setLikes] = useState(0);
const [optimisticLikes, setOptimisticLikes] = useOptimistic(likes);
async function likeAction() {
setOptimisticLikes(likes + 1);
const saved = await saveLike(likes + 1);
startTransition(() => {
setLikes(saved);
});
}
return (
<form action={likeAction}>
<button type="submit">いいね {optimisticLikes}</button>
</form>
);
}
I did not wrap setOptimisticLikes in startTransition, yet no error appeared, and the button stayed at the optimistic count of 1 right after the click, just as it did with the startTransition version. According to the useOptimistic reference example that uses optimistic state in action props, a function passed to a prop that accepts an action is already called inside startTransition. The <form> reference section on handling form submission with an action prop also says that the function passed to action runs inside a transition.
Inside startTransition, a setTimeout callback still causes the error
Even when you are inside startTransition, calling the update function from a setTimeout callback caused the error.
function handleClick() {
startTransition(() => {
setTimeout(async () => {
setOptimisticLikes(likes + 1);
// The rest is the same as the startTransition example above
}, 0);
});
}
The message was a single line starting with the same An optimistic state update occurred outside a transition or action. as the first example. The button text also flashed and reverted: 0ms "いいね 0" → 4ms "いいね 1" → 5ms "いいね 0" → 1004ms "いいね 1". As described in the caveats section of the startTransition reference, the function passed to startTransition runs immediately, and only updates that happen during that call are treated as a transition. The setTimeout callback runs later, so it falls outside the transition.
On the other hand, when I called setOptimisticLikes after await Promise.resolve() inside an async function passed to startTransition, there was no error, and the display stayed at 1 like. However, every example in the reference sets the optimistic value before the first await, so writing it that way is clearer.
The production build shows no error, but the display still reverts
I ran npm run build, opened the result with npx vite preview, and tested the incorrect BadClick component. No error appeared in the console, but the button text still changed as it did on the development server: 0ms "いいね 0" → 3ms "いいね 1" → 3ms "いいね 0" → 1002ms "いいね 1". The react-dom production code and the built output did not contain the message string itself.
In production, a button that does not change for a second after you press it runs without any message. If you see this error during development, move the call into startTransition or into the form's action right away.




