Search code examples
javascriptmobxmobx-state-tree

Mobx-state-tree: how to rename AnonymousModel to the store name


i am using mobx-state-tree and i would like to actually show the store name when an error appears instead of AnonymousModel.

For example:

const SignupModel = types
  .model({
    isUsingFacebook: false,
    birthday: '',
    timeOfBirth: types.maybeNull(types.string),
    placeOfBirth: types.maybeNull(types.string),
    gender: types.maybeNull(types.string),
    password: '',
  })

Still gives me an error like

Error while converting {...} to AnonymousModel.

But i would like to get

Error while converting {...} to SignupModel.

Solution

  • Simply pass a desired name as a first argument to model method, like so:

    const SignupModel = types
      .model('SignupModel', {
        isUsingFacebook: false,
        birthday: '',
        timeOfBirth: types.maybeNull(types.string),
        placeOfBirth: types.maybeNull(types.string),
        gender: types.maybeNull(types.string),
        password: '',
      })