lemmur-app-android/lib/hooks/observed.dart

21 lines
556 B
Dart
Raw Normal View History

2020-09-30 01:19:44 +02:00
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:mobx/mobx.dart';
/// Observes MobX observables in [fn] and returns the built value.
2020-09-30 19:05:00 +02:00
/// When observable inside have changed, the hook rebuilds the value.
2020-09-30 01:19:44 +02:00
/// The returned value can be ignored for a `useEffect(() { autorun(fn); }, [])`
2020-09-30 19:05:00 +02:00
/// effect.
2020-09-30 01:19:44 +02:00
T useObserved<T>(T Function() fn) {
final returnValue = useState(useMemoized(fn));
useEffect(() {
final disposer = autorun((_) {
returnValue.value = fn();
});
return disposer;
}, []);
return returnValue.value;
}