Skip to content

When to call makeAutoObservable? #2831

Answered by urugator
williamrjribeiro asked this question in Q&A
Discussion options

You must be logged in to vote

makeAutoObservable/makeObservable makes observable only existing properties:

class Doubler {  
  constructor(val) {    
    this.value = val || 0;
    makeAutoObservable(this);    
  }
}

You can use property initializer, but you have to assign some initial value:

class Doubler {  
  value = 0
  constructor(val) {    
    makeAutoObservable(this);    
  }
}

Because:

class Doubler {  
  value;
  constructor(val) {    
    console.log(this.hasOwnProperty('value')) // false
    makeAutoObservable(this);   
  }
}

Replies: 1 comment 4 replies

Comment options

You must be logged in to vote
4 replies
@mweststrate
Comment options

@williamrjribeiro
Comment options

@phoenixeliot
Comment options

@phoenixeliot
Comment options

Answer selected by williamrjribeiro
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
4 participants