Git Product home page Git Product logo

Comments (3)

reboottime avatar reboottime commented on June 9, 2024

Rationale

image

Vs

Redux diagram

image

from angular-playbook.

reboottime avatar reboottime commented on June 9, 2024

Example variations

from angular-playbook.

reboottime avatar reboottime commented on June 9, 2024

State level

angular supports generate feature state using cli

In NgRx, StoreModule.forRoot() and StoreModule.forFeature() are methods to configure state management for an Angular application, each serving distinct purposes

  1. StoreModule.forRoot():

    • to set up the root application state. It should be called only once in your application, typically in the AppModule.

    • configures the global state for your application and initializes the top-level state slices.

    • The first argument to forRoot() is the initial state of the application, and the second argument is a configuration object (optional).

    • Example usage:

      import { StoreModule } from '@ngrx/store';
      
      @NgModule({
        imports: [
          // ...
          StoreModule.forRoot({ /* initial state */ }),
          // ...
        ],
        // ...
      })
      export class AppModule { }
  2. StoreModule.forFeature():

    • Tto set up feature modules. Feature modules are used to organize the state of specific features or parts of your application.

    • Called once for each feature module and allows you to add additional state slices to your application.

    • Example usage:

      import { StoreModule } from '@ngrx/store';
      
      @NgModule({
        imports: [
          // ...
          StoreModule.forFeature('featureName', /* feature reducer */),
          // ...
        ],
        // ...
      })
      export class FeatureModule { }
    • In the above example, 'featureName' is a unique string identifier for this feature module, and /* feature reducer */ is the reducer function for this feature's state slice.

    • You can use createFeatureSelector and createSelector to access the state of the feature module.

  3. When to Use Each:

    • forRoot: Use this in the root module of your application (typically AppModule) to set up the global state for your application. It initializes the top-level state slices and should only be called once.

    • forFeature: Use this in feature modules to add additional state slices. Feature modules are typically used to manage the state for specific parts of your application. You can call forFeature multiple times to set up state for different features.

In summary, forRoot is used in the root module to configure the global application state, while forFeature is used in feature modules to manage specific parts of the application's state. This helps in organizing and modularizing the state management in NgRx applications.

from angular-playbook.

Related Issues (20)

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.