Loading...
function Save({ data }) {
const [loading, setLoading] = useState(false);
async function onClick() {
setLoading(true);
await save(data);
setLoading(false);
}
return <button disabled={loading} onClick={onClick}>Save</button>;
}function Save({ data }) {
const [isPending, startTransition] = useTransition();
const onClick = () => startTransition(async () => {
await save(data);
});
return <button disabled={isPending} onClick={onClick}>Save</button>;
}The transition tracks pending state for you, and it resets even when save() throws, unlike the manual flag.
isLoading state next to an action, which drifts out of sync with isPending.startTransition or a <form action>, not in onSubmit with manual useState loading flags.isPending from useTransition or useActionState.An Action is any function run inside a transition. A Server Action is an Action that is a Server Function, so its body runs on the server.
Yes, but wrap updates after await in another startTransition, or they are not marked as part of the transition.
No, useActionState returns whatever the action returns, so an error message can be part of that state.