Scalable Signals Angular Architecture with NGRX Signal Store
course
This course is perfect for those looking for a lightweight alternative to NgRx Store that scales effortlessly.
Browse
Find courses, lessons, articles, talks, and podcasts.
8 results for "ngrx"
course
This course is perfect for those looking for a lightweight alternative to NgRx Store that scales effortlessly.
article
Over the years, your application have grown huge. Sometimes, really HUGE. Many developers have contributed to the codebase. One day they pushed one coding style, a different one another day. Sometimes even different patterns and solutions for similar problems. You want to keep your codebase consistent. In the Angular ecosystem, there were well established patterns for doing things the right way ™️. But the old solutions are being challenged by signal-based approach. Eventually, you ask yourself the key question: **Should you integrate with the NEW solutions or stick to the battle-tested ones, but already considered LEGACY**? That's a very good question indeed. Don't rush with architectural decisions 🤓. ## First, think of what problems you want to solve by introducing the change. Do you have 1k LoC Angular components? Maybe even 2k? Do you and your team enjoy working with these? 😏 Where does the **domain logic and state management code** belong to - do you have a clear guidance on which code should be put where? Have you spot that different devs implement similar features in totally different ways? Do you keep on asking the same questions over and over again: **whether this certain piece of code** should be **imperative** (plain old object properties and variables) or **reactive** (streams)? It'd be nice to keep all thinga reactive, but more often than not, it introduces complexity. ## Second, think of long-term consequences. You are going to have more than one way to **manage the state**. But is that really an issue? If your codebase is modular, separate modules should not depend on each other in any way. Inner abstractions of one should be invisible to the other, etc. You are going to introduce a **new tool**. On one hand, you need to learn it, and so does your team. But on the other hand, it's a breathe of fresh air. And it's useful to question the way you've been doing things so far. Maybe there's a more effective way? You are going to embrace signal-based APIs. And that means taking taking advantage of Zoneless Angular. This means using all modern Angular APIs. And you know that signals are the future. Why would you want to stick to the **less performant, less type-safe, less flexible and, objectively, less effective solutions**? ## Finally, plan your INCREMENTAL integration. INCREMENTAL is the absolute keyword here. You don't want to tell your PM you need to stop delivering features because of a refactor. Actually, there's no need for a "refactor". Just pick the first new feature to be implemented. Or pick the one that needs to be extended. NGRX Signal Store allows you to create both very simple & small stores, as well as bigger ones. **Composition** is the key here. **Start simple**. Create a `withState` section with the signals that hold the state. That's already reactive. And relatively simple. ```ts const initialState: CartState = { items: ..., rebate: ..., } export const CartStore = signalStore( withState(initialState) ); ``` Create a `withMethods` section to provide ways to change the state: ```ts export const CartStore = signalStore( ... withMethods((store) => ({ addItem(item: ShoppingItem): void { patchState(store, (state) => ({ items: [ ...state.items, item ] })); }, ... ); ``` Create a `withComputed` section to provide all derived computations: ```ts export const CartStore = signalStore( ... withComputed(({ items }) => ({ totalPrice: computed(() => items().reduce(...)) ... ); ``` **Start simple**. Now you can (literally) **`inject` this code into your existing solution**. All you need is to inject your brand new NGRX Signal Store implementation into a component, or a service: ```ts @Injectable() class ExistingBigService { cartStore = inject(CartStore) //... } ``` and that's it! What's beatiful is that NGRX Signal Store should remain an implementation details (of a component, service, etc). It **shouldn't leak outside to the entire application unintentionally**. Small NGRX Signal Stores help us to **keep logic more modular**. Don't hesitate - give NGRX Signal Store a try! Start with the [_**Scalable Signals Angular Architecture with NGRX Signal Store**_](https://egghead.io/courses/scalable-signals-angular-architecture-with-ngrx-signal-store-a33abd39) course now!
podcast
<p>John talks with Juri Strumpflohner, an industry expert and angular trainer; and Rob Wormald, an Angular core development team member, getting into how Angular has evolved with the 2.0 release, powerful new features, their favorite libraries, and where the future is taking it.</p><p>Angular has gotten much better under the hood. Rob talks about how the Angular team is working on really improving the code while still keeping the public API stable. He also talks about the team's ongoing debate on where to improve the code. Faster? Smaller? Currently, the team has chosen to work on making it smaller and has improved the bundle size of Angular.</p><p>One of the new things about Angular that people are most excited about is the Elements and CLI Schematics libraries. Juri talks about how Elements opens up a "whole new world," allowing people not to have to resolve the same problems over and over again by letting them create reusable angular components.</p><p>One of the hardest things to learn with Angular was the design and architecture patterns. Rob goes into how the team has improved the documentation, now actually getting into best practices and giving architecture guidelines.</p><p>Finally, our guests get into their favorite Angular libraries. NgRx Formly being the big favorite. NgRx Formly is a beautiful library that allows devs to create powerful reactive forms. Rob also highly recommends the Angular Schematics library. It is a powerful low-level tool that allows you to create templates and code generators. You can even use it in conjunction with the Angular CLI to extend it or modify it for your own needs!</p><hr /><h3>Transcript</h3><p><a href="https://egghead.simplecast.com/episodes/b58b2c84/transcript">"Angular Web Applications with Juri Strumpflohner and Rob Wormald (Angular Core Team)" Transcript</a></p><h3>Resources</h3><ul><li><a href="https://angular.io/">Angular 2</a></li><li><a href="https://github.com/robwormald/angular-elements">Angular Elements</a></li><li><a href="https://www.npmjs.com/package/@angular-devkit/schematics">Angular Schematics</a></li><li><a href="https://github.com/ngrx/platform">NgRx</a></li><li><a href="https://angular.io/guide/reactive-forms">Angular Reactive Forms</a></li></ul><h3>Rob Wormald</h3><ul><li><a href="https://twitter.com/robwormald?lang=en">Twitter</a></li><li><a href="https://github.com/robwormald">Github</a></li><li><a href="https://egghead.io/instructors/rob-wormald">Egghead Courses</a></li></ul><h3>Juri Strumpflohner</h3><ul><li><a href="https://twitter.com/juristr?lang=en">Twitter</a></li><li><a href="https://juristr.com/">Blog</a></li><li><a href="https://egghead.io/instructors/juri-strumpflohner">Egghead Courses</a></li></ul>
lessonDuration 5m 12s
ngrx/store is a library that simplifies common RxJS patterns for managing state and gives you an easy api to use it within your Angular application. This lesson shows how to convert a common startWith and scan stream into an ngrx Store and reducer.
lessonDuration 1h 22mFree
Joel and Mark Erikson chat about the motivation behind Redux. The historical context and the future of Redux. How Redux Toolkit solves community pain points. Additionally, Mark Erikson gives a walkthrough of how Redux Toolkit works and how to use createSlice, createAsyncThunk, and createEntityAdapter. ## Key Terms **Redux** - Redux is an open-source JavaScript library for managing application state. **Immer** - a library created based on the “copy-on-write” mechanism — a technique used to implement a copy operation in on modifiable resources. **Flux** - is an architecture for creating data layers in JavaScript applications. It was designed at Facebook along with the React library. **Redux Toolkit** - an add-on package to Redux, which includes opinionated defaults. ## Resources - [https://twitter.com/acemarke](https://twitter.com/acemarke) - [Redux Toolkit](https://redux-toolkit.js.org) - [Mark's Dev Blog](https://blog.isquaredsoftware.com) - [Redux - A predictable state container for JavaScript apps](https://redux.js.org) ## What keeps you excited about Redux? - The motivation: people are still using Redux and will continue to use Redux for a long time. ## How do people criticize Redux? - This blog post was a result of responding to the endless waves of “Redux is dead...” - People don’t understand the context and history behind why a particular tool exists—for example, jQuery, Angular, Backbone, etc. - All these tools were created at a specific time, in a particular place, by people trying to solve particular problems. - And they were written using the knowledge that existed at that particular time. - Understanding the context will help you know where it belongs in the larger picture. - **Example of this:** "Things You Should Never Do, Part I" by Joel Spolsky. “When you throw away code and start from scratch, **you are throwing away all that knowledge**. All those collected bug fixes. Years of programming work.” ## Redux in 2020 - In the **context of Redux**, it came out in 2015. And currently, people ask: - Why do I have to dispatch things? - Why is it called actions? - Why do I need to have separation between data and UI? - Here, it’s essential to understand that Flux inspired Redux. - Like Flux, Redux prescribes that you concentrate your model update logic in a particular layer of your application. Instead of letting the application code directly mutate the data, both tell you to describe every mutation as a plain object called an “action.” - The idea was to have access to a single function and not worry about synchronization. - People learning Redux today don’t see the historical context of those patterns. - **Another criticism of Redux:** verbosity. - The reality is that Redux lets us choose how verbose we’d like your code to be, depending on personal style, team preferences, longer-term maintainability, and so on. - The **three fundamental principles** of Redux has been over-interpreted: - **Single source of truth**: The state of your whole application is stored in an object tree inside a single store. - **State is read-only**: The only way to mutate the state is to emit an action, an object describing what happened. - **Mutations are written as pure functions**: To specify how the state tree is transformed by actions, you write pure reducers. ## Redux Toolkit - People have assumed that they need to put everything into Redux. But that’s overkilled. It’s quickly going to become unmaintainable. - Link to doc: [When should I use Redux?](https://redux.js.org/faq/general#when-should-i-use-redux) - The approach to dealing with Boilerplate in Redux was to use Immer. - **Michel Weststrate** builds Immer - simplifies writing immutable update logic: [Docs:](https://immerjs.github.io/immer/docs/introduction) “The basic idea is that you will apply all your changes to a temporary draftState, which is a proxy of the currentState. Once all your mutations are completed, Immer will produce the nextState based on the mutations to the draft state.” [Michel Weststrate creator of Mobx and Immer Libraries for JavaScript](https://egghead.io/podcasts/michel-weststrate-creator-of-mobx-and-immer-libraries-for-javascript) - Putting everything together changed how Redux Toolkit was created. It addressed three common concerns about Redux: 1. “Configuring a Redux store is too complicated.” 2. “I have to add a lot of packages to get Redux to do anything useful.” 3. “Redux requires too much boilerplate code.” - Redux Toolkit was built to be beneficial to all Redux users, not just someone new to Redux. - It’s very opinionated. ## What are the big ideas of State Management? - First, ask yourself, what type of app am I building? - Where is the data coming in from? - How am I interacting with that data? - How long does the data need to live? - Does the data need to change over time? - Those questions will dictate what tools to use. - An **example** of this: Building a MEAN.js AngularJS Project with Create-React-App. ## What does the future look like for Redux? - "Redux is here to stay..." - Many downloads are coming from Wordpress’s Gutenberg (block editor) and other larger projects such as Gatsby. During Gatsby’s bootstrap & build phases, the state is stored and manipulated using the Redux library. The fundamental purpose of using Redux in Gatsby’s internals is to centralize all of the state logic. - Lots of tools overlap with Redux. It has passed the peak of popularity, and usage will shrink over time. But at the same time, people are going to keep using Redux. Technologies don’t just disappear. - **Questions to be answered:** - How will Redux fit with Concurrent Mode and other features from the React team? - Redux will probably make use of `useMutableSource` for compatibility purposes. [useMutableSource](https://github.com/facebook/react/pull/18000) - enables React components to safely and efficiently read from an unreliable external source in Concurrent Mode. The API will detect mutations that occur during render to avoid tearing, and it will automatically schedule updates when the source is mutated. ## Redux Toolkit The recommended way to start new apps with React and Redux Toolkit is by using the official Redux+JS template for Create React App. - The first problem that it’s trying to solve: it takes too many steps to set up a store. - The solution is to create Reducers that specify how the application’s state changes in response to actions sent to the store. - Actions only describe what happened but don’t explain how the application’s state changes. - With Flux, they had individual stores. In Redux, you have a single Object decided up. - It’s generally agreed that functions should be relatively short and ideally only do one specific thing. - One key/value section of that object is a “slice,” We use the term “slice reducer” to describe the reducer function responsible for updating that slice of the state. ### Structure files as "Ducks": - Redux itself does not care about how your application’s folders and files are structured. However, co-locating logic for a given feature in one place makes it easier to maintain that code. - Redux Toolkit addressed a pain point from the community: having several files for one feature. ## Working with `createSlice` As `createSlice` creates your actions and your reducer for you, you don’t have to worry about type safety here. Action types can just be provided inline. ## Working with `createAsyncThunk` - This API focuses on creating a standard recommended approach for handling async request lifecycles. Link to documentation of `createAsyncThunk`: - If you need to cancel a `thunk` before the payload creator is called, you may provide a condition callback as an option after the payload creator. ## Working with `createEntityAdapter` - It provides a set of functions that handles the “entity state” structure. - This API was ported from the `@ngrx/entity library` created by the NgRx maintainers and significantly modified for Redux Toolkit. - You can decide what `actions` to pass. And the CRUD methods may be used in multiple ways. ## Pull Request #3740 - The Redux documentation needs to be re-written. The current documentation was written in 2015; it’s no longer relevant for new developers. - **The Master List.** - Goals: - Just Enough of Redux. - Learn the basics of Redux and the fastest way to get going, + “the right way.” - Show enough real-world patterns that you can build an app. ## Instructional Design - Egghead has focused on taking subject matter experts and systematically designing, developing, and delivering instructional products. - The outputs can be a video course, documentation, etc.; what’s important is the output. - There is no downside to having an instructional design session on the Redux documentation. ## Roam Research - Roam as a tool for networked thought. - Powered by bi-directional links. ## Redux Toolkit 1.0 "Redux is not designed to be the most performant or the most concise way of writing mutations. Its focus is on making the code predictable." - **Tradeoffs of React Hooks:** [https://www.youtube.com/watch?v=xiKMbmDv-Vw&feature=youtu.be](https://www.youtube.com/watch?v=xiKMbmDv-Vw&feature=youtu.be) **Recap:** It's about tradeoffs. Neither approach is "right" or "wrong" - it's about what tradeoffs you want to make: - How much do you want to "separate concerns"? - Do you prefer "shallow" testing of components, or fully "mounting/integrating" them? - How important are shallow component trees and static typing? [Source to Slides.](https://blog.isquaredsoftware.com/presentations/2019-09-hooks-hocs-tradeoffs/#/16)
course
***Note: RxJS imports have changed since the publication of this course. The concepts being taught on RxJS are still applicable.** Angular has many built-in features for working closely with RxJS to help you build entirely reactive applications. This course will teach you how to use Angular with RxJS. We will learn how to use the ngrx/store library to manage state in a clean, reactive "Redux style." This course assumes you've got a working knowledge of Angular and reactive programming with RxJS. If you don't, you might want to watch the following courses to get you up to speed! - [Get Started with Angular](https://egghead.io/courses/get-started-with-angular) - [Introduction to Reactive Programming with RxJS]( https://egghead.io/series/introduction-to-reactive-programming). - [Step by Step Async Programming with RxJS](https://egghead.io/series/step-by-step-async-javascript-with-rxjs) - [Getting Started With Redux](https://egghead.io/series/getting-started-with-redux), a course by Redux's, Dan Abramov. We also have Intermediary Angular courses if you'd like to acquire more in-depth knowledge. - [Dependency Injection](https://egghead.io/courses/angular-2-dependency-injection-di-explained - [Components, Directives](https://egghead.io/courses/building-angular-components) - [Forms](https://egghead.io/courses/intro-to-angular-forms) - [Router](https://egghead.io/courses/intro-to-angular-router) - [Animating Web Apps](https://egghead.io/courses/animate-angular-web-applications)
lessonDuration 3m 38s
This lesson summarizes everything the course has covered and how Angular 2, RxJS, and ngrx/store work together to achive a reactive application.
lessonDuration 10m 49sFree
@ngrx/store builds on the concepts made popular by Redux and supercharges it with the backing of RxJS. The result is a tool and philosophy that will transform the way you approach state management in your Angular 2 applications. This lesson takes an existing Angular 2 app and refactors it to utilize @ngrx/store, touching on all of the major concepts along the way!