mobxjs / mobx-angular

The MobX connector for Angular.
MIT License
483 stars 59 forks source link

how to handle change value mobx store in component.ts #83

Closed Parinyadahmi closed 5 years ago

Parinyadahmi commented 5 years ago

import {Component, OnChanges, OnInit, ChangeDetectionStrategy, ChangeDetectorRef} from '@angular/core'; import {UserStore} from '../stores/user.store';

@Component({ selector: 'app-portfolio', templateUrl: './portfolio.component.html', styleUrls: ['./portfolio.component.css'] }) export class PortfolioComponent implements OnInit, OnChanges {

constructor(public user: UserStore) {
}

ngOnInit() {
}

ngOnChanges(changes: any) {
    console.log('change');
}

}

adamkleingit commented 5 years ago

Hi, you can use reaction or autorun from MobX: https://mobx.js.org/refguide/reaction.html https://mobx.js.org/refguide/autorun.html

gelinger777 commented 2 years ago

dear @adamkleingit I have tried to add a reaction to my store by

 reaction(
          () => this.accelerometer,
          accelerometer => {
            console.log('TTTTTTTTTTTTTTTTTTTTTT', accelerometer);
            this.recalculateScannerData();
          },
          { fireImmediately: true }
        );

but it triggers only once. On future updates of accelerometer object it is not fired. Any idea why?