Giter Club home page Giter Club logo

Comments (5)

hggeorgiev avatar hggeorgiev commented on August 31, 2024

Hello rkusuma!
Yes, first, you select the state from the application state:

export function getCurrencies(state$: Observable<State>) {
  return state$.select(state => state.currencies);
}

Then, you can compose selections of the properties within the state (ex. getting the currencies array)

export const getCurrencyEntities = compose(fromCurrencies.getCurrenciesEntities , getCurrencies);

When you select from the state within a component/directive/pipe/service, you have to use the selector from the root, not the selectors from the state you need:

//CORRECT
  this.currencies = this._store.let(fromRoot.getCurrencyEntities);
//INCORRECT (will throw an exception)
  this.currencies = this._store.let(fromCurrencies.getCurrencies));

I hope this answers your question!

from ngx-redux-app.

rkusuma avatar rkusuma commented on August 31, 2024

my reducer is quite deep... so it would harder to create selector function on each reducer.

Example State

{
  a: {
    b: {
      c: {
        xx: 10,
        yy: 'Hi'
      },
      ...
    },
    ...
  },
  ...
}

to get the value xx from c reducer i must create selector function in:

  • c reducer
  • b reducer
  • a reducer

and it's possible to c reducer have many state.

from ngx-redux-app.

hggeorgiev avatar hggeorgiev commented on August 31, 2024

In this case, I would have a single reducer and a state (for a), and handle all the selections and changes within the a.reducer.ts and then expose them to the root reducer.

You can also compose multiple selectors in the root reducer as seen here between lines 146 and 152.

from ngx-redux-app.

rkusuma avatar rkusuma commented on August 31, 2024

yes, that's what i mean.

// app.reducer.ts
export interface State {
  moduleA: fromModuleA.State,
  moduleB: fromModuleB.State,
  ...
  moduleD: fromModuleD.State
}

export const getModuleAState = (state) => state.moduleA;
export const getEntities = createSelector(getModuleAState, fromModuleA.getEntities); 
// moduleA.reducer.ts
export interface State {
  subModuleAA: fromSubModuleAA.State,
  subModuleAA: fromSubModuleAA.State
}

export const getSubModuleAAState = (state) => state.subModuleAA;
export const getEntities = createSelector(getModuleAState, fromSubModuleAA.getEntities);
// sub-module-aa.reducer.ts
export interface State {
  entities: Enttities[],
  ...
}

export const getEntities = (state) => state.entities;

as you can see, i need to create 3 getEntities selector function to get entities from SubModuleAA reducer to App reducer.
there is still module B, module C, and module D.

is it expected to create chain selector function?

from ngx-redux-app.

hggeorgiev avatar hggeorgiev commented on August 31, 2024

Well, this is the way to go. You can also use a chain selector to select everything from module A (using composition) if you wish instead of adding a separate selector for each of the substates.

from ngx-redux-app.

Related Issues (5)

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.