Skip to content

types.compose and lifecycle methods #1885

Answered by EmilTholin
mortmoe asked this question in Q&A
Discussion options

You must be logged in to vote

Hi @mortmoe!

You could use the approach outlined in the Simulate inheritance by using type composition part of the documentation and save a reference to the "super" testMethod and call that manually in testMethod in Noteable:

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

const Note = types.model("note", {
  id: types.identifierNumber,
  description: types.string
});

const Noteable = types
  .model({
    notes: types.optional(types.array(Note), [])
  })
  .actions((self) => {
    const superTestMethod = self.testMethod;

    return {
      afterCreate() {
        console.log("After create in Notable");
      },
      testMethod() {
        if (superTestMethod) superTestMethod();

        console

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@mortmoe
Comment options

@tony
Comment options

Answer selected by mortmoe
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