Fetching latest headlinesโ€ฆ
useOptimistic + useActionState: React 19 Killed 50 Lines of My Boilerplate
NORTH AMERICA
๐Ÿ‡บ๐Ÿ‡ธ United Statesโ€ขJune 28, 2026

useOptimistic + useActionState: React 19 Killed 50 Lines of My Boilerplate

0 views0 likes0 comments
Originally published byDev.to

Every form submission in React used to look like this:

const [data, setData] = useState(null)
const [loading, setLoading] = useState(false)
const [error, setError] = useState(null)

async function handleSubmit(e) {
  e.preventDefault()
  setLoading(true)
  try {
    const result = await submitToServer(formData)
    setData(result)
  } catch (err) {
    setError(err.message)
  } finally {
    setLoading(false)
  }
}

Three useState calls. A try/catch/finally. Manual loading and error flags. And that's without optimistic updates โ€” add those and you're looking at another 20 lines of snapshot/revert logic.

React 19 ships two hooks that delete most of this.

useActionState โ€” form lifecycle in one hook

const [error, submitAction, isPending] = useActionState(
  async (prevState, formData) => {
    const res = await fetch('/api/submit', {
      method: 'POST',
      body: formData,
    })
    if (!res.ok) return 'Something went wrong'
    return null
  },
  null
)

return (
  <form action={submitAction}>
    <input name="email" type="email" />
    <button disabled={isPending}>
      {isPending ? 'Sending...' : 'Submit'}
    </button>
    {error && <p>{error}</p>}
  </form>
)

isPending is automatic. Error state is the return value of your action. No useState, no try/catch wrappers, no finally.

useOptimistic โ€” instant UI without manual rollback

The old way of doing a like button:

// snapshot โ†’ update โ†’ revert on error = ~30 lines

The React 19 way:

const [optimisticLikes, addOptimisticLike] = useOptimistic(
  likes,
  (current, increment) => current + increment
)

async function handleLike() {
  addOptimisticLike(1)      // instant UI
  await likePost(postId)    // server confirms
}

return <button onClick={handleLike}>โค๏ธ {optimisticLikes}</button>

That's it. If the server call fails, React automatically reverts to the real value โ€” no manual rollback code needed. The optimistic state is just a temporary layer that disappears when the action settles.

One important gotcha

addOptimistic must be called before any await in your action. React's transition system only captures optimistic updates issued synchronously before the first suspension point. Call it after an await and the update won't show up.

// โœ… correct
addOptimisticLike(1)
await likePost(postId)

// โŒ won't work
await someOtherThing()
addOptimisticLike(1)

When to still use React Query / SWR

These hooks are for component-local action state. If your mutation needs to invalidate a cache shared across multiple components, you still want React Query or similar. But for the submit-and-respond pattern that covers the majority of forms and mutations in a typical app โ€” these built-ins are now the right default.

Comments (0)

Sign in to join the discussion

Be the first to comment!