Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using makeAutoObservable before variable initialization doesnt trigger state changes #3815

Closed
Lucheti opened this issue Jan 14, 2024 · 3 comments
Labels

Comments

@Lucheti
Copy link

Lucheti commented Jan 14, 2024

Intended outcome:
Created a class, used makeAutoObservable. therefore, a observer component should re-render the ui when variable changes

Actual outcome:
ui didnt re-render

How to reproduce the issue:
this class is a singleton for stores:

export class AppStore {

  private static instance: AppStore

  authStore: AuthStore;

  postStore: PostStore;

  counter: Counter;

  private constructor() {
    makeAutoObservable(this)
    this.authStore = new AuthStore()
    this.postStore = new PostStore()
    this.counter = new Counter()
  }

  public static getInstance() {
    if (!AppStore.instance) {
      AppStore.instance = new AppStore();
    }
    return AppStore.instance;
  }
}

this is the postStore:

export class PostStore {
  posts: Post[];

  isLoading: boolean;

  constructor() {
    makeAutoObservable(this)
    this.posts = []
    this.isLoading = false
  }

  *fetchPosts() {
    this.isLoading = true
    const postsResponse = yield fetch('/api/posts')
    const posts = yield postsResponse.json()
    console.log("fetchPosts ", posts)
    this.posts = posts.map((post: PrismaPost) => new Post(post));
    console.log("fetchPosts ", this.posts)
    this.isLoading = false
  }

  *createPost({ title, content }: { title: string, content: string }): Generator<unknown, void, Post> {
    const post = yield fetch('/api/posts', {
      method: 'POST',
      body: JSON.stringify({ title, content })
    })
    this.posts.push(post)
  }
}

that doesnt work but moving the makeAutoObservable(this) to the end of the constructor in the postStore fixes the issue

Idk if its a bug or the intended way to work, but its not in the docs of the fn.

https://github.com/Lucheti/test-mobx-prisma-nextjs

Versions

@urugator
Copy link
Collaborator

Please make sure your compiler is set up properly
https://mobx.js.org/installation.html#use-spec-compliant-transpilation-for-class-properties

@Colin3191
Copy link

I'm having the same problem.

@kubk
Copy link
Collaborator

kubk commented Sep 30, 2024

@Colin3191 Please read the comment above. It includes link to a snippet that allows you to check whether compiler is configured correctly: https://mobx.js.org/installation.html#use-spec-compliant-transpilation-for-class-properties:~:text=For%20verification%20insert%20this%20piece%20of%20code

Closing the issue as no response was provided.

@kubk kubk closed this as completed Sep 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants