JS Built-ins
redux-toolkitcreateActionbeginner

createAction

πŸ“– Usage Example
const { createAction } = ReduxToolkit
const increment = createAction('counter/increment')
increment()       // β†’ { type: 'counter/increment' }
increment.toString() // β†’ 'counter/increment'

createAction returns a typed action creator β€” the passed argument automatically becomes the payload.

createAction

createAction(type) returns an action creator function. Calling it with no arguments produces { type }. Passing an argument sets { type, payload: arg }.

Challenge: Create increment and addBy action creators.

increment()        // β†’ { type: 'counter/increment' }
addBy(5)           // β†’ { type: 'counter/addBy', payload: 5 }
increment.toString() // β†’ 'counter/increment'
πŸ’‘ Hints (3)
  • β€’ `createAction("counter/increment")` returns a function that creates `{ type: "counter/increment" }`.
  • β€’ The action creator's `.toString()` method returns the type string β€” useful for switch statements.
  • β€’ The argument passed to the action creator becomes `action.payload` automatically.