Skip to content

ObservableObject@2.init? being read outside a reactive context #2722

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

You must be logged in to vote

Note sure why I didn't try makeAutoObservable with the factory function and only with a Class but doing so made the warning go away.

Code: https://codesandbox.io/s/makeautoobservable-init-read-outside-forked-2yirn?file=/src/App.tsx

export const App = observer(() => {
  const [localStore] = useState(() =>
    makeAutoObservable({
      name: "",

      init(name: string) {
        this.name = name;
      }
    })
  );

  useEffect(() => {
    localStore.init("CodeSandbox");
  }, [localStore]);

  return (
    ...
  );
});

I will change my code to use makeAutoObservable.

Replies: 3 comments 3 replies

Comment options

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

@codeBelt
Comment options

Comment options

You must be logged in to vote
0 replies
Comment options

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

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