简体   繁体   中英

Calling action from another action

I have a sub-store as

info: {}, 
data: {},
items: [],

I have created 3 actions to reset the above shown individual properties:

resetInfo:action((state, payload) => {
    state.info = {};
  }),
resetData: action((state, payload) => {
    state.data = {};
  }),
resetItems:action((state, payload) => {
    state.items = [];
  }),

I want to create another action let say reset or resetAll which internally calls the above 3 actions as:

reset:action((state, payload) => {
    resetInfo();
    resetData();
    resetItems();
}),

but I am getting error as these methods can't be found. So is there a way I can call the 3 actions inside reset action?

I know alternatively, I can write my reset as shown below, but I am loling for the solution with the above approach

reset:action((state, payload) => {
    state.info = {};
    state.data = {};
    state.items = [];
}),

Simply create a different function:

function setInState(state, p, value) {state[p] = value}

Then call setInState from all your actions...

If you insist on using the actions without a separate function, they are properties of another object and not standalone variables. So you would fetch them from the object.

So maybe this.resetData() may work. But my first recommendation is generally more robust...

Also, 'this' will not even work in an arrow function; so you may need to change other things...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM