Skip to content

addDisposer not called after test completion? #2106

Answered by cmdcolin
cmdcolin asked this question in Q&A
Discussion options

You must be logged in to vote

Actually found a self answer, somewhere high up in my tree I can call destroy in a useEffect

import { addDisposer, destroy, types } from "mobx-state-tree";
import { useEffect, useState } from "react";

const model = types
  .model({
    a: 1,
    b: 2,
  })
  .actions((self) => ({
    afterCreate() {
      console.log("create");
      addDisposer(self, () => {
        console.log("destroy"); // never called
      });
    },
  }));

export default function App() {
  const [myModel, setMyModel] = useState();
  useEffect(() => {
    const m = model.create();
    setMyModel(m);
    return () => {
      destroy(m); // causes the addDisposer to be called
    };
  }, []);
  return <div>Hello wor…

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by cmdcolin
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant