Class NgRedux<RootState>

The NgRedux class is a Redux store implementation that can be used in Angular applications. It extends the AbstractStore class from Redux.

Type Parameters

  • RootState = any

    The root state of the store.

Hierarchy

Constructors

  • Constructor

    Ng Zone

    • control angular change detection base on zoneJS api that intersect async operation like setTimeOut, setInterval, click and many more. use angular ngzone wrapper allow control change detection trigger.

    Type Parameters

    • RootState = any

    Parameters

    • Optional ngZone: NgZone

    Returns NgRedux<RootState>

Properties

_store: Store<RootState, any>

Private property that holds the application's store object. The Store is an object that holds the application's state tree. There should only be a single store in a Redux app, as the composition happens on the reducer level.

Template

The root state of the application's store

Template

the type of actions which may be dispatched by this store

_store$: ReplaySubject<any> = ...

Angular subject store corresponds to store change event and trigger rxjs change event

ngZone?: NgZone

Accessors

Methods

  • Configures the store with the provided reducer, initial state, middleware, and enhancers. This should only be called once for the lifetime of your app, for example, in the constructor of your root component.

    Example

    export class AppModule {
    constructor(ngRedux: NgRedux<IAppState>) {
    // Tell @angular-redux2/store about our rootReducer and our initial state.
    // It will use this to create a redux store for us and wire up all events.
    ngRedux.configureStore(rootReducer, INITIAL_STATE);
    }
    }

    Returns

    Throws

    Throws an error if the store has already been initialized.

    Parameters

    • reducer: Reducer<RootState>

      The root reducer function of the store.

    • initState: RootState

      The initial state of the store.

    • Optional middleware: Middleware<any, AnyAction>[] = []

      The middleware functions to be applied to the store.

    • Optional enhancers: StoreEnhancer<RootState>[] = []

      The store enhancers to be applied to the store.

    Returns void

  • Configures a sub-store with a specified base path and local reducer. Carves off a 'subStore' or 'fractal' store from this one.

    The returned object is itself an observable store, however, any selections, dispatches, or invocations of localReducer will be specific to that sub-store and will not know about the parent ObservableStore from which it was created.

    This is handy for encapsulating component or module state while still benefiting from time-travel, etc.

    Example

      onInit() {
    // The reducer passed here will affect state under `users.${userID}`
    // in the top-level store.
    this.subStore = this.ngRedux.configureSubStore(
    ['users', userId],
    userComponentReducer,
    );

    // Substore selections are scoped to the base path used to configure
    // the substore.
    this.name$ = this.subStore.select('name');
    this.occupation$ = this.subStore.select('occupation');
    this.loc$ = this.subStore.select(s => s.loc || 0);
    }

    Returns

    The sub-store service instance.

    Type Parameters

    • SubState

      The type of the sub-store state.

    Parameters

    • basePath: PathSelector

      The base path of the sub-store.

    • localReducer: Reducer<SubState>

      The local reducer of the sub-store.

    Returns SubStoreService<SubState>

  • Dispatches an action to the store instance. A dispatching function (or simply dispatch function) is a function that accepts an actions or an async actions; it then may or may not dispatch one or more actions to the store.

    We must distinguish between dispatching functions in general and the base dispatch function provided by the store instance without any middleware.

    The base dispatch function always synchronously sends an actions to the store's reducer, along with the previous state returned by the store, to calculate a new state. It expects actions to be plain objects ready to be consumed by the reducer.

    Example

     class App {
    @Select() count$: Observable<number>;

    constructor(private ngRedux: NgRedux<IAppState>) {}

    onClick() {
    this.ngRedux.dispatch({ type: INCREMENT });
    }
    }

    Returns

    • the dispatched action object.

    Throws

    • if the store instance is not initialized.

    Description

    This method dispatches an action object to the store instance. If the store instance is not initialized, it will throw an error. The dispatch method runs in the Angular zone by default to prevent unexpected behavior when dispatching from callbacks to 3rd-party.

    Type Parameters

    • Action extends AnyAction<Action>

      the action object type.

    Parameters

    • action: Action

      the action object to be dispatched to the store.

    Returns Action

  • Get the current state from the store.

    Example

      incrementIfOdd(): void {
    const { counter } = this.ngRedux.getState();
    if (counter % 2 !== 0) {
    this.increment();
    }
    }

    Returns

    The current state of the store.

    Returns RootState

  • Accepts a Redux store, then sets it in NgRedux and allows NgRedux to observe and dispatch to it. This should only be called once for the lifetime of your app, for example, in the constructor of your root component. If configureStore has been used, this cannot be used.

    Example

    class AppModule {
    constructor(ngRedux: NgRedux<IAppState>) {
    ngRedux.provideStore(store, rootReducer);
    }
    }

    Returns

    void

    Throws

    Error if the store has already been initialized.

    Parameters

    • reducer: Reducer<RootState>

      The new root reducer function to use.

    • store: Store<RootState, AnyAction>

      The Redux store instance to be initialized.

    Returns void

  • Replaces the current root reducer function with a new one.

    Example

    const newRootReducer = combineReducers({
    existingSlice: existingSliceReducer,
    newSlice: newSliceReducer
    })

    ngRedux.replaceReducer(newRootReducer)

    Returns

    Parameters

    • nextReducer: Reducer<RootState>

      The new root reducer function to be used.

    Returns void

  • Select a slice of state to expose as an observable.

    Example


    constructor(private ngRedux: NgRedux<IAppState>) {}

    ngOnInit() {
    let { increment, decrement } = CounterActions;
    this.counter$ = this.ngRedux.select('counter');
    }

    Returns

    An Observable that emits items from the source Observable with distinct values

    Type Parameters

    • SelectedType

      The type of the selected slice of state

    Parameters

    • Optional selector: Selector<any, SelectedType>

      A key or function to select a part of the state

    • Optional comparator: Comparator

      A comparison function called to test if an item is distinct from the previous item in the source

    Returns Observable<SelectedType>

  • Adds a change listener to the store, which will be invoked any time an action is dispatched, and some part of the state tree may potentially have changed.

    Example

      constructor(
    private ngRedux: NgRedux<IAppState>,
    private actions: CounterActions,
    ) {
    this.subscription = ngRedux
    .select<number>('count')
    .subscribe(newCount => (this.count = newCount));
    }

    ngOnDestroy() {
    this.subscription.unsubscribe();
    }

    Returns

    A function to remove this change listener.

    Parameters

    • listener: (() => void)

      A callback function that will be invoked whenever the state in the store has changed.

        • (): void
        • Returns void

    Returns Unsubscribe

Generated using TypeDoc