react icon indicating copy to clipboard operation
react copied to clipboard

Bug: `useActionState`'s second argument is not optional in the React types

Open cpojer opened this issue 3 weeks ago • 3 comments

React version: 19.2

Steps To Reproduce

  1. useActionState(action) currently throws a type error.

The current behavior

I'm building a library that makes use of React Actions for mutations. There is no need to pass an initial state variable for useActionState. An "implicit undefined" initial state is perfectly fine and saves people from typing 6 characters each time.

When I omit the second argument, React appears to be working just fine. However, @types/react expects the second argument to be provided. Can we make it optional or is there a reason to keep it required?

The expected behavior

useActionState(action) should not throw a type error.

I submitted a PR for @types/react here: https://github.com/DefinitelyTyped/DefinitelyTyped/pull/74156

cpojer avatar Nov 28 '25 07:11 cpojer

cc @eps1lon @rickhanlonii @sophiebits (who appear to have had major input on useActionState's design based on past PRs)

cpojer avatar Nov 28 '25 07:11 cpojer

We don't allow implicit undefined in useState, useContext etc either. The docs also mark the initial state as required: https://react.dev/reference/react/useActionState

eps1lon avatar Nov 29 '25 16:11 eps1lon

My specific use case is that I'm using a framework that provides actions for useActionState, like:

const […] = useActionState(frameworkProvidedAction, null);

The framework handles all the action code and does not need access to the previous state. However, as a user I have to pass the initial value unnecessarily every time. I assume in 90%+ of cases the user does not need to set the initial result of the action to anything but null/undefined.

Another solution is to provide a hook that wraps useActionState, but that's more API surface for very little benefit.

cpojer avatar Nov 30 '25 03:11 cpojer