I am learning ngxs but I can't understand when should I use patchState and setState? What's the difference?
const state = ctx.getState();
let data = this.service.list();
ctx.setState({
...state,
feedAnimals: data
});
vs.
let data = this.service.list();
ctx.patchState({
feedAnimals: data
});
Those two pieces of code are equivalent.
patchStateis just a short hand version of thesetState({...state, ... }code.In future
patchStatewill most likely be evolving to a more useful immutability helper with equality testing (ie. the state would only be changed if the patch actually changes any values) and patch operators (this is still in discussion).I would recommend using
patchStatefor neatness and to take advantage of features that are on their way.