当前位置:网站首页>很流行的状态管理库 MobX 是怎么回事?
很流行的状态管理库 MobX 是怎么回事?
2022-06-10 21:05:00 【InfoQ】
MobX 简介
observablesreactionsobservablesreactionsreactions核心元素
ObservablesActionsReactionsCounter
Observables
Observablesobservablesimport 'package:mobx/mobx.dart';
final counter = Observable(0);
observablesclass Counter {
Counter() {
increment = Action(_increment);
}
final _value = Observable(0);
int get value => _value.value;
set value(int newValue) => _value.value = newValue;
Action increment;
void _increment() {
_value.value++;
}
}
*.g.dartbuilder_runnermobx_codegen*.g.dartflutter packages pub run build_runner build
import 'package:mobx/mobx.dart';
part 'counter.g.dart';
class Counter = CounterBase with _$Counter;
abstract class CounterBase with Store {
@observable
int value = 0;
@action
void increment() {
value++;
}
}
observable@observable@readonlysetContactfirstNamelastNameContactfullNamefirstNamelastNameComputed Observables@computedimport 'package:mobx/mobx.dart';
part 'contact.g.dart';
class Contact = ContactBase with _$Contact;
abstract class ContactBase with Store {
@observable
String firstName;
@observable
String lastName;
@computed
String get fullName => '$firstName, $lastName';
}
Actions
Actionsobservablesactionsvalue++increment()actionsactionactionsactionfinal counter = Observable(0);
final increment = Action((){
counter.value++;
});
actions@action//...
abstract class CounterBase with Store {
//...
@action
void increment() {
value++;
}
}
runInAction@observable
String stuff = '';
@observable
loading = false;
@action
Future<void> loadStuff() async {
loading = true; //This notifies observers
stuff = await fetchStuff();
loading = false; //This also notifies observers
}
Reactions
ReactionsobservablesactionsreactionsReactionsobservableReactionsReactionDisposerreactionReactionsobservablereactionobservablesReactions方式一,autorun方法。
import 'package:mobx/mobx.dart';
String greeting = Observable('Hello World');
final dispose = autorun((_){
print(greeting.value);
});
greeting.value = 'Hello MobX';
// Done with the autorun()
dispose();
// 打印结果:
// Hello World
// Hello MobX
Reactions方式二,reaction方法:
ReactionDisposer reaction<T>(T Function(Reaction) predicate, void Function(T) effect)
predicateobservablespredicateeffectpredicateobservablesimport 'package:mobx/mobx.dart';
String greeting = Observable('Hello World');
final dispose = reaction((_) => greeting.value, (msg) => print(msg));
greeting.value = 'Hello MobX'; // Cause a change
// Done with the reaction()
dispose();
// 打印结果:
// Hello MobX
- Reactions 方式三,when 方法:
ReactionDisposer when(bool Function(Reaction) predicate, void Function() effect)
predicatetrueeffecteffectreactionreactionimport 'package:mobx/mobx.dart';
String greeting = Observable('Hello World');
final dispose = when((_) => greeting.value == 'Hello MobX', () => print('Someone greeted MobX'));
greeting.value = 'Hello MobX'; // Causes a change, runs effect and disposes
// Prints:
// Someone greeted MobX
- Reactions 方式四,Future 异步方法:
Future<void> asyncWhen(bool Function(Reaction) predicate)
final completed = Observable(false);
void waitForCompletion() async {
await asyncWhen(() => _completed.value == true);
print('Completed');
}
Observer
reactionsObserverflutter_mobxbuildobservablesobservablesObserver总结
MobXActionsVSCode
边栏推荐
- Ajout, suppression et modification des données du tableau [MySQL] (DML)
- Array remove duplicates from an array
- [nk] Niuke monthly competition 51 f-average question
- Interpreting the new ecology of education in maker space
- IDEA出现“XXX has broken path”报错解决方法
- leetcode:333. 最大 BST 子树
- [NK] question de calcul de 51 g pour le match lunaire Bullock
- Business based precipitation component = & gt; manage-table
- An analysis of SQL query optimization principle (900w+ data from 17s to 300ms)
- Mysql 什么是聚集索引和非聚集索引?
猜你喜欢
随机推荐
系统重装以及查询系统性能
Understanding of related concepts of target detection
PHP pseudo protocol implementation command execution details
Share this great God's WPF interface design series video
Variables (automatic variables, static variables, register variables, external variables) and memory allocation of C malloc/free, calloc/realloc
Shaping teenagers' comprehension ability with children's programming thinking
C language - quick sorting in sorting
变量(自动变量、静态变量、寄存器变量、外部变量)与C的内存分配malloc/free、calloc/recalloc
Abbexa AML1 DNA binding ELISA Kit instructions
About type-C
GMPNN:Drug-drug interaction prediction with learnable size-adaptive molecular substructures.
ArrayList的扩容机制
Abbkine column exkine Pro animal cell / tissue Total Protein Extraction Kit
Abbexa low sample size chicken lysozyme C (Lyz) ELISA Kit
Kdd2022 | neural network compression of depth map based on antagonistic knowledge distillation
解读创客空间下的教育新生态
Constructing the implementation strategy of steam education for children
报错解决Error parsing Mapper XML
The system needs to scan all the files and try to identify the cover of the video
C语言-排序中的快速排序(简称快排)



![[nk] Niuke monthly competition 51 f-average question](/img/b3/c36a0032e606f38fdc2f7c4562713c.png)




