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

What is the reason to use @observable in the TodoModel.ts #10

Open
changLiuUNSW opened this issue Oct 4, 2017 · 1 comment
Open

What is the reason to use @observable in the TodoModel.ts #10

changLiuUNSW opened this issue Oct 4, 2017 · 1 comment
Labels

Comments

@changLiuUNSW
Copy link

changLiuUNSW commented Oct 4, 2017

Here is TodoModel.ts:

import { observable } from 'mobx';

export class TodoModel {

  readonly id: number;
  @observable public text: string;
  @observable public completed: boolean;

  constructor(text: string, completed: boolean = false) {
    this.id = TodoModel.generateId();
    this.text = text;
    this.completed = completed;
  };

  static nextId = 1;
  static generateId() {
    return this.nextId++;
  }
}

export default TodoModel;

I am new to Mobx, just want to know why we need use @observable inside model.
I think they are redundant because we already use @observable inside TodoStore.ts.

export class TodoStore {
......
  @observable
  public todos: Array<TodoModel>;
......

I have tried remove @observable from the model, the application still work

@rokoroku
Copy link
Owner

rokoroku commented Oct 7, 2017

You're right. it doesn't need to observable on TodoModel's member property (since it only uses todos array), but when you need to change existing todo model's value, it will be required.

It's better to read the official docs: https://mobx.js.org/best/react.html

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

2 participants