How make instance of class observable (mobx)

Viewed 238

I have react component and I need make instance of class like observable.

@observer
class A extend React.Component {
    // make observable
    instance = new B();

    render (){
        <div>
            <div onClick={() => this.instance.setName('Joe')}/>
            // should update
            <div>{this.instance.name}</div>
        </div>
    }
}

Class that we inject:

class B {
    @observabe
    _name: string

    @action
    setName(name: string) {
        this._name = name;
    }
}
1 Answers

Okay, I just forgot to bind context. You can use either arrow function or @action.bind.

Related