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

Multiple observables in reaction #800

Open
vytautas-pranskunas- opened this issue Apr 27, 2022 · 1 comment
Open

Multiple observables in reaction #800

vytautas-pranskunas- opened this issue Apr 27, 2022 · 1 comment

Comments

@vytautas-pranskunas-
Copy link

vytautas-pranskunas- commented Apr 27, 2022

Hello,

is there a way to wathch multiple observables in reaction similar to combineLatest? I see in your documentation

reaction
ReactionDisposer reaction<T>(T Function(Reaction) fn, void Function(T) effect)
**Monitors the observables** used inside the fn() tracking function and runs the effect() when the tracking function returns a different value. Only the observables inside fn() are tracked.

but i do not see any example how to add multiple observables into function.
I need it because i have to trigger changes on initial items load and after on subject change.

@amondnet
Copy link
Collaborator

@vytautas-pranskunas-

  final greeting = Observable('Hello World');
  final greeting2 = Observable('Hello World');

  // 1, reaction
  final dispose =
      reaction((_) => [greeting.value, greeting2.value], (values) => print(values));
  // 2. when
  final dispose2 = when(
      (_) => greeting.value == 'Hello MobX' || greeting2.value == 'Hello MobX',
      () => print('Someone greeted MobX'));

  runInAction(() => greeting.value =
      'Hello MobX'); // Causes a change, runs effect and disposes
// Prints:
// Someone greeted MobX
// [Hello MobX, Hello World]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants