NgRx Ducks
  • Introduction
  • @ngrx-ducks/core
    • Installation
    • Architecture
      • @StoreChunk
      • createDuck
      • getReducer
      • createMutableDuck
      • getMutableReducer
      • useSelect
      • useSelectors
      • useActions
    • Guides
      • Quick Start
  • Migrations
    • v15
  • Resources
  • FAQ
  • GitHub
  • Changelog
  • Sponsor
Powered by GitBook
On this page
  • Selector
  • Selector with props

Was this helpful?

  1. @ngrx-ducks/core
  2. Architecture

useSelect

PreviousgetMutableReducerNextuseSelectors

Last updated 3 years ago

Was this helpful?

A facade can be enhanced with usePick, which can proceed and transforms them into consumable Observable-streams.

This allows the facade to read data from different State-Slices that are not part of the facade itself.

Selector

To set up useSelect a property needs to be added to the facade that is assigned with usePick(). In the Component, each and every selector of the project can be passed to pick. The result type of the selector is inferred automatically and provides the correct type information.

import { useSelect, StoreFacade } from '@ngrx-ducks/core';

@StoreChunk()
export class Chunk {
  select = useSelect();
}
import { Component, OnInit } from '@angular/core';
import { Chunk } from './chunk';
import { greetings } from './selector';

@Component({ /* ... */})
export class SomeComponent implements OnInit {
  greetings: Observable<string[]>;
  
  constructor(private chunk: Chunk) {}
  
  ngOnInit() {
    this.greetings = this.chunk.select(greetings);
  }
}
import { createFeatureSelector, createSelector } from '@ngrx/store';

const feature = createFeatureSelector<State>('some');

export const greetings = createSelector(
  feature,
  state => state.greetings
);
export interface State {
  greetings: string[];
}

Selector with props

Pick also accepts selectors having properties (). The pick-Function than expects a second parameter representing the properties (see component.ts).

import { createDuck, useSelect } from '@ngrx-ducks/core';

@StoreChunk()
export class Chunk {
  select = useSelect();
}
import { Component, OnInit } from '@angular/core';
import { Chunk } from './chunk';
import { greetingsIncluding } from './selector';

@Component({ /* ... */})
export class SomeComponent implements OnInit {
  greetings: Observable<string[]>;
  
  constructor(private chunk: Chunk) {}
  
  ngOnInit() {
    this.greetings = this.facade.select(greetingsIncluding, { term: 'good'});
  }
}
import { createFeatureSelector, createSelector } from '@ngrx/store';

const feature = createFeatureSelector<State>('some');

export const greetingsIncluding = createSelector(
  feature,
  (state, props: { term: string }) => state.greetings.filter(g => 
    g.includes(props.term)
  )
);
export interface State {
  greetings: string[];
}
NgRx's selectors
selectors with props