Skip to content

mobx-react(-lite) useLocalObservable will wrap a class instance in observable if make(Auto)Observable is not used #2579

@AoDev

Description

Context

I am migrating my boilerplates

  • from mobx v4/v5 mobx-react v6
  • to mobx v6, mobx-react v7

Intended outcome

I switched to mobxReact.useLocalObservable().

For some complex components, I have classes for local state.
So I have adapted the code to have calls like mobxReact.useLocalObservable(() => new MyState())

While most of the time, make(Auto)Observable will be used, I also had the case where a class "in WIP" was just passing non observable data.

import * as mobx from 'mobx'

export default class AppSettingsVM {
  constructor ({rootStore}) {
    mobx.makeAutoObservable(this) // <- WITHOUT this line, the instance is turned into an observable and the app crashes
    this.settings = rootStore.coreStore.settings
  }
}

Actual outcome:

From the docs, if useLocalObservable returns a class instance, it should be kept as is, which is not what is happening here and leads to a crash.

How to reproduce the issue:

Use useLocalObservable where it returns a class instance that is not using mobx make(Auto)Observable.

Versions
mobx v6, mobx-react v7

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions