Skip to content

Private actions #1884

Answered by EmilTholin
wtuminski asked this question in Q&A
Mar 11, 2022 · 2 comments · 5 replies
Discussion options

You must be logged in to vote

I agree with @elektronik2k5 and use the same convention myself.

If you really don't want to expose internal actions, you can hide them in the scope similar to what you can do with volatile state, granted they are synchronous.

import { types } from "mobx-state-tree";

let Store = types
  .model({
    num: types.number,
    log: types.array(types.number)
  })
  .actions((self) => {
    const _logNum = (num: number) => {
      self.log.push(num);
    };

    return {
      setNum(num: number) {
        self.num = num;
        _logNum(num);
      }
    };
  });

const store = Store.create({ num: 1, log: [] });

store.setNum(2);
console.log(store); // { num: 2, log: [2] }
// Property '_logNum'…

Replies: 2 comments 5 replies

Comment options

You must be logged in to vote
1 reply
@wtuminski
Comment options

Comment options

You must be logged in to vote
4 replies
@wtuminski
Comment options

@wtuminski
Comment options

@EmilTholin
Comment options

@wtuminski
Comment options

Answer selected by wtuminski
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants