本文介绍了如何在NGXS中使用patchState与setState?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习ngxs,但我不明白何时应使用patchStatesetState?有什么区别?

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
});

推荐答案

这两个代码是等效的.patchState只是setState({...state, ... }代码的简写版本.

Those two pieces of code are equivalent.patchState is just a short hand version of the setState({...state, ... } code.

将来,patchState很可能会通过相等性测试(例如,状态仅在补丁实际更改任何值时才更改)和补丁运算符(仍在讨论中)发展成为更有用的不变性助手.

In future patchState will 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).

我建议使用patchState来保持整洁并充分利用即将使用的功能.

I would recommend using patchState for neatness and to take advantage of features that are on their way.

这篇关于如何在NGXS中使用patchState与setState?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 19:12