DEV Community

Alfredo Perez
Alfredo Perez

Posted on

4 1

NGRX Workshop Notes - Meta-Reducers

  • Intercept actions before they are reduced
  • Intercept state before it is emitted
  • Can change the control flow of the Store

Alt Text

Most common use cases

  • Reset state when a signout action occurs
  • for debugging creating logger
  • to rehydrate when the application starts up

-It is like a plugin system for the store, they behave similarly to the interceptors

Example

An example of this can be to use it in a logger

const logger = (reducer: ActionReducer<any, any>) => (state: any, action: Action) => {
    console.log('Previous State', state);
    console.log('Action', action);

    const nextState = reducer(state, action);

    console.log('Next State', nextState);
    return nextState;
};

export const metaReducers: MetaReducer<State>[] = [logger];
Enter fullscreen mode Exit fullscreen mode

Top comments (2)

Collapse
 
amirensit profile image
choubani amir

How to understand this line ?
const logger = (reducer: ActionReducer) => (state: any, action: Action) => { }
There is two "=>".
Any idea ?

Collapse
 
docker1 profile image
Agafonov Vladislav

function accepting reducer returns function that accepts state and action and returns new state?🤷‍♂️

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay