当前位置:网站首页>很流行的状态管理库 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
边栏推荐
- 数组 加一
- 【MySQL】常见数据类型总结
- Use of C language qsort() function (detailed explanation)
- 笔记(四)- 多线程
- An analysis of SQL query optimization principle (900w+ data from 17s to 300ms)
- Detailed steps and actual records of SQL server2019 installation (available for hands-on test)
- [nk] Niuke monthly race 51g calculation problem
- Latex error: file ‘xxx.sty‘ not found
- Part 7: Lesson 2 general skills of consultants - how to install and uninstall SAP ERP system client
- Understanding of related concepts of target detection
猜你喜欢
一次SQL查询优化原理分析(900W+数据从17s到300ms)

构建幼儿Steam教育实施策略

GMPNN:Drug-drug interaction prediction with learnable size-adaptive molecular substructures.

C语言-排序中的快速排序(简称快排)

Before we learn about high-performance computing, let's take a look at its history

NFT版权/版税

Abbexa cdan1 siRNA instruction manual

GMPNN:Drug-drug interaction prediction with learnable size-adaptive molecular substructures.

What are MySQL clustered indexes and nonclustered indexes?

在模仿学习中进步的智能机器人
随机推荐
[nk] 牛客月賽51 G計算題
JS mobile terminal copy text to clipboard code
C#使用S7.net连接西门子S1200PLC,C#直接连接西门子PLC
Modify frontsortinglayer variable of spritemask
Array plus one
【MySQL】常见数据类型总结
C语言判断文件或文件夹是否存在
笔记(二)
Introduction to abbexa bacterial genome DNA Kit
Understanding of related concepts of target detection
Principle of gravure overprint and factors affecting overprint
数组 是否存在重复元素
Variables (automatic variables, static variables, register variables, external variables) and memory allocation of C malloc/free, calloc/realloc
[1024 ways to play windows azure] 75 Fast cloud database migration seamlessly migrate alicloud RDS SQL server to azure SQL databas
PHP pseudo protocol implementation command execution details
【phpstorm】 No data sources are configured to run this SQL and provide advanced c
C语言-排序中的快速排序(简称快排)
Are you still writing the TS type code
NFT copyright / royalties
系统重装以及查询系统性能