Search code examples
angulartypescriptreduxngrx

Is it possible to create a reducer without the createReducer function in ngrx


I am going through an existing code and found that there is a reducer created without the use of createReducer function syntax.

This reviewReducer function from the code snippet below takes the same number of arguments like a reducer and same type of arguments and responds to an action as well (verified after keeping a debugger).

But surprisingly I am not able to find createReducer function either in create-reducer.ts file or in the entire app. So I want to check if we can create a reducer without it?

create-reducer.ts

import { Action } from 'rxjs/scheduler/Action';

export function reviewReducer(state: State<Any>, action: ReviewActionList{

  return {
    ...state,
    result: true
  }
}

Solution

  • Yes, it is possible to create a reducer without using the createReducer() function. The createReducer() function is a convenience function that can be used to create reducers that follow a particular pattern. However, it is not necessary to use this function.

    To create a reducer without using createReducer(), you can simply define a function that takes two arguments: the current state and the action. The function should then return the new state.

    Keep in mind function should be a pure function

    Read more about reducers here