Git Product home page Git Product logo

ngrx-basics's Introduction

NgRx Basics

Implementación del patrón Redux en aplicación angular

App Contador implementación de comunicación mediante @Input @Output vs NgRx

alt text


Conceptos y ejemplos sobre el manejo Action, Reducer, State, Store

Redux

  • La información se encuentra en una estructura previamente definida.

  • Toda la información estará almacenada en el STORE

  • El STORE no se modifica de forma directa

  • Cualquier interacción con el STORE se realiza mediante el disparo (DISPATCH) de acciones ( ACTIONS )

  • El estado actual de la información se llama STATE

  • Un nuevo estado es creado con base a la combinación del estado anterior (OldState) y una acción (ACTION) a traves de una función pura llamada REDUCER


Action

export const increment = createAction("[Contador] Increment");
export const decrement = createAction("[Contador] Decrement");
export const multiplicar = createAction(
  "[Contador] Multiplicar",
  props<{ numero: number }>()
);
export const dividir = createAction(
  "[Contador] Dividir",
  props<{ numero: number }>()
);
export const reset = createAction("[Contador] Reset");

Reducer

const _counterReducer = createReducer(
  initialState,
  on(increment, (state) => state + 1),
  on(decrement, (state) => state - 1),
  on(multiplicar, (state, { numero }) => state * numero),
  on(dividir, (state, { numero }) => state / numero),
  on(reset, (state) => (state = initialState))
);

export function counterReducer(state: any, action: Action) {
  return _counterReducer(state, action);
}

State

const initialState = 20;

Store

Responsabilidades del Store:

  • Permite la lectura del estado mediante la función getState();
  • Permite la creación de un nuevo estado mediante la función dispatch( action: Action )
  • Permite la notificación de cambiaos mediante la subscripción.
// app.module
StoreModule.forRoot({ contador: counterReducer }),


// custom feature...

  constructor(private store: Store<AppState>) {}

  ngOnInit(): void {
    this.store.select('contador').subscribe((state) => (this.contador = state));
  }

  incrementar() {
    this.store.dispatch(actions.increment());
  }
  decrementar() {
    this.store.dispatch(actions.decrement());
  }

Demo

NgRx Demo

ngrx-basics's People

Contributors

carlospaterninadev avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.