Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NGXS updateItem state operator

Tags:

ngxs

I'm trying to use the NGXS state operators inside of my application, but I'm having trouble finding good examples of how to use them for slightly more complex updates.

For example, NGXS's documentation shows an example of updating this state:

@State<AnimalsStateModel>({
  name: 'animals',
  defaults: {
    zebras: ['Jimmy', 'Jake', 'Alan'],
    pandas: ['Michael', 'John']
  }
})

In order to change the names of one of the pandas, it uses NGXS's "updateItem" state operator like this:

@Action(ChangePandaName)
  changePandaName(ctx: StateContext<AnimalsStateModel>, { payload }: ChangePandaName) {
    ctx.setState(
      patch({
        pandas: updateItem(name => name === payload.name, payload.newName)
      })
    );
  }

In this example, the updateItem function uses a lambda expression in its first parameter to find the correct object in the array and replaces it with with the object in the second parameter.

How would you do this with an array containing complex objects of which you only wanted to change the value of one property? For instance, what if my state was this:

@State<AnimalsStateModel>({
      name: 'animals',
      defaults: {
        zebras: [{1, 'Jimmy'} , {2, 'Jake'}, {3, 'Alan'}],
        pandas: [{1, 'Michael'}, {2, 'John'}]
      }
    })

How would I use the updateItem function to locate the correct animal using the ID and then update the name?

like image 820
darkknightdetec Avatar asked Mar 11 '19 19:03

darkknightdetec


People also ask

How do I update my state in NGXS?

The NGXS patchState method is used to do immutable object updates to the container state slice without the typical long-handed syntax. This is very neat and convenient because you do not have to use the getState and setState as well as the Object. assign(...) or the spread operator to update the state.

What is NGXS?

NGXS is a state management pattern and library for Angular. NGXS acts as a single source of truth for your application's state - providing simple rules for predictable state mutations. NGXS is modeled after the CQRS pattern - a pattern implemented in state management libraries such as NgRx and Redux.


1 Answers

The default state example you provided is invalid syntax, but I think I get what you intended to provide. Something like this:

@State<AnimalsStateModel>({
  name: 'animals',
  defaults: {
    zebras: [{id: 1, name: 'Jimmy'} , {id: 2, name: 'Jake'}, {id: 3, name: 'Alan'}],
    pandas: [{id: 1, name: 'Michael'}, {id: 2, name: 'John'}]
  }
})

updateItem also accepts a state operator as a second parameter so you could use the patch operator again to modify the item. Your action would then look like this:

@Action(ChangePandaName)
changePandaName(ctx: StateContext<AnimalsStateModel>, { payload }: ChangePandaName) {
  ctx.setState(
    patch({
      pandas: updateItem(item=> item.id === payload.id, patch({ name: payload.newName }))
    })
  );
}
like image 71
Mark Whitfeld Avatar answered Jan 02 '23 21:01

Mark Whitfeld