Fireship · @fireship

Angular ngrx Redux Quick Start Tutorial

Uploaded August 29, 2017 · Length 9:01

Angular ngrx Redux Quick Start Tutorial

Summary

This video teaches how to implement Redux with NGRX in an Angular app, covering the basics of Redux and building a simple app from scratch.

  • angular ngrx redux tutorial
  • ngrx store example
  • angular redux implementation
  • ngrx store setup
  • angular ngrx store

Transcript preview

The first 2 minutes of the transcript. Select a timestamp to watch that moment on YouTube.

  1. 0:00

    Redux is a design pattern that keeps all application data in a single JavaScript object.

  2. 0:05

    This gives you a predictable global data state at any given point in time.

  3. 0:09

    This technique is most popular in React apps, but it can also be applied to Angular apps

  4. 0:14

    with a library called NGRX, which uses Redux principles with RxJS as the underlying library.

  5. 0:20

    In this episode, I'm going to cover the basics of Redux, and then we'll build an Angular

  6. 0:25

    app from scratch using NGRX.

  7. 0:27

    Let's start by looking at the high-level principles of Redux.

  8. 0:30

    So we have a user interface that is displaying some data.

  9. 0:34

    Then the user performs an action that will change the state of the data.

  10. 0:37

    In Redux, the only way to change the state is by dispatching an action.

  11. 0:42

    Once an action has been dispatched, it will go to a reducer function, which will copy

  12. 0:46

    the current state of the application, along with any data changes, to a new JavaScript

  13. 0:50

    object.

  14. 0:51

    It's important to note that the state is immutable, meaning it can't be changed directly.

  15. 0:55

    has to be copied over to a whole brand new state.

  16. 0:58

    The reason for this is it gives us a predictable tree of state changes that we can use to debug

  17. 1:03

    an application, and that's really the main benefit of Redux.

  18. 1:06

    Once the reducer has created the new state, it gets saved in a datastore, which can be

  19. 1:10

    thought of as a client-side database more or less.

  20. 1:13

    In NGRX, we treat this datastore as an observable, so we can subscribe to it from anywhere in

  21. 1:18

    the application.

  22. 1:19

    This means that all of our components and services will be sharing the same data at

  23. 1:23

    any given point in time.

  24. 1:25

    Now let's start building a brand new NGRX app.

  25. 1:29

    First run ng-new and then npm install ngrx slash store.

  26. 1:33

    I'm going to start with the most simple example possible, where our entire application state

  27. 1:37

    is represented by a single string.

  28. 1:39

    Then we'll create two different actions to translate the string into Spanish or French.

  29. 1:44

    First let's go into the app module and import NGRX.

  30. 1:48

    I'm also importing a reducer function which we'll create in the next step.

  31. 1:52

    Then in the imports metadata add storeModuleForRoot and then we'll add a key message with the simple

  32. 1:58

    reducer.

  33. 1:59

    Now let's go ahead and build this reducer function.