The right way of using ngrx/store to update store

angular, ngrx, redux, state

Solution

You should do it using @ngrx/effects.

- Component dispatches an action: LOGIN

- Effect catches the action.

- Effect triggers the service to get the data.

- Effect returns a new action with the data.

- Reducer gets the new action with data as payload and builds the state.

This is the best practice.

See my repo for examples of using effects.

Memroy Game (with effects)

Problem

I'm learning ngrx/store and I have a component that call to a service to get some data from the server, as I refactor it to use ngrx/store, I don't know where to update the store. As I understand I have 2 options: - Call the service from the component, get the data, and use `dispatch` to update to store. - Call the service from the component, and the service will update the store state using `dispatch`. The component can subscribe to that part of the state (using `select`) and when the service will get the data and update the state, the component will get the update through the store subscription. Which is the right ("Best Practice") way to go? (Maybe there is another why I should do this?)

Original source