createAction
Last updated
Last updated
Generates an action creator for a specific action type.
createAction
takes an action type value and returns an which produces actions of that type.
For a version of the action creator which accepts a payload
value and attaches the returned action, call the withPayload()
method (e.g., createAction("…").withPayload()
.)
In addition to withPayload()
, action creators returned by createAction()
have the following properties:
type
: The type value passed to createAction()
. Removes the need for a separate action type constant, and allows other helpers such as to inspect the type
value of the produced actions at runtime.
matches(action)
: A method that returns true the the passed action has the same type
as the ones produced by the action creator.
.withPayload()
is defined with a type parameter that specifies the payload type. You can override the default (any
) by specifying the type parameter explicitly:
.matches()
is defined as a . Using it in a condition allows the TypeScript compiler to narrow the type of passed action to the specific type of action returned by the action creator:
Defining a basic action:
Defining an action with payload:
Specifying the action payload type (TypeScript):
guide