Skip to main content

[ ![Redux Logo](/img/redux.svg) ![Redux Logo](/img/redux.svg) **Redux
Toolkit** ](/)

[ Getting Started ](/introduction/getting-started) [ Tutorials
](/tutorials/overview) [ Usage Guide ](/usage/usage-guide) [ API
](/api/configureStore) [ RTK Query ](/rtk-query/overview) [ GitHub
](https://github.com/reduxjs/redux-toolkit)

Search

  * [ Introduction ](/introduction/getting-started)

    * [ Getting Started ](/introduction/getting-started)
    * [ Why Redux Toolkit is How To Use Redux Today ](/introduction/why-rtk-is-redux-today)
  * [ Tutorials ](/tutorials/overview)

    * [ Tutorials Overview ](/tutorials/overview)
    * [ Quick Start ](/tutorials/quick-start)
    * [ TypeScript Quick Start ](/tutorials/typescript)
    * [ RTK Query Quick Start ](/tutorials/rtk-query)
  * [ Using Redux Toolkit ](/usage/migrating-to-modern-redux)

    * [ Migrations ](/usage/migrating-to-modern-redux)

    * [ Usage Guide ](/usage/usage-guide)
    * [ Usage With TypeScript ](/usage/usage-with-typescript)
    * [ Writing Reducers with Immer ](/usage/immer-reducers)
    * [ Setup with Next.js ](/usage/nextjs)
  * [ API Reference ](/api/configureStore)

    * [ Store Setup ](/api/configureStore)

      * [ configureStore ](/api/configureStore)
      * [ getDefaultMiddleware ](/api/getDefaultMiddleware)
      * [ Immutability Middleware ](/api/immutabilityMiddleware)
      * [ Serializability Middleware ](/api/serializabilityMiddleware)
      * [ Action Creator Middleware ](/api/actionCreatorMiddleware)
      * [ createListenerMiddleware ](/api/createListenerMiddleware)
      * [ createDynamicMiddleware ](/api/createDynamicMiddleware)
      * [ getDefaultEnhancers ](/api/getDefaultEnhancers)
      * [ autoBatchEnhancer ](/api/autoBatchEnhancer)
    * [ Reducers and Actions ](/api/createReducer)

      * [ createReducer ](/api/createReducer)
      * [ createAction ](/api/createAction)
      * [ createSlice ](/api/createSlice)
      * [ createAsyncThunk ](/api/createAsyncThunk)
      * [ createEntityAdapter ](/api/createEntityAdapter)
      * [ combineSlices ](/api/combineSlices)
    * [ Other ](/api/createSelector)

      * [ createSelector ](/api/createSelector)
      * [ Matching Utilities ](/api/matching-utilities)
      * [ Other Exports ](/api/other-exports)
      * [ Codemods ](/api/codemods)
      * [ Error Messages ](/errors)
  * [ RTK Query ](/rtk-query/overview)

    * [ RTK Query Overview ](/rtk-query/overview)
    * [ Comparison with Other Tools ](/rtk-query/comparison)
    * [ Examples ](/rtk-query/usage/examples)
    * [ Usage With TypeScript ](/rtk-query/usage-with-typescript)
    * [ Using RTK Query ](/rtk-query/usage/queries)

    * [ API Reference ](/rtk-query/api/createApi)

  * [ ](/)
  * API Reference 
  * Reducers and Actions 
  * createReducer 

On this page

#  ` createReducer() `

##  Overview  ​

A utility that simplifies creating Redux reducer functions. It uses Immer
internally to drastically simplify immutable update logic by writing
"mutative" code in your reducers, and supports directly mapping specific
action types to case reducer functions that will update the state when that
action is dispatched.

Redux [ reducers ](https://redux.js.org/basics/reducers) are often implemented
using a ` switch ` statement, with one ` case ` for every handled action type.

    
    
    const initialState = { value: 0 }  
      
    function counterReducer(state = initialState, action) {  
      switch (action.type) {  
        case 'increment':  
          return { ...state, value: state.value + 1 }  
        case 'decrement':  
          return { ...state, value: state.value - 1 }  
        case 'incrementByAmount':  
          return { ...state, value: state.value + action.payload }  
        default:  
          return state  
      }  
    }  
    

This approach works well, but is a bit boilerplate-y and error-prone. For
instance, it is easy to forget the ` default ` case or setting the initial
state.

Th