forked from mobxjs/mobx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
1235 lines (765 loc) · 71.7 KB
/
Copy pathindex.html
File metadata and controls
1235 lines (765 loc) · 71.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE HTML>
<html lang="en" >
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>Introduction | MobX</title>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<meta name="description" content="<img src="docs/mobx.png" alt="logo" height="120" align="right" />">
<meta name="generator" content="GitBook 2.6.9">
<meta name="HandheldFriendly" content="true"/>
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
<link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">
<link rel="stylesheet" href="gitbook/style.css">
<link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-edit-link/plugin.css">
<link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-anchors/plugin.css">
<link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-highlight/website.css">
<link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-search/search.css">
<link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-fontsettings/website.css">
<link rel="next" href="./intro/overview.html" />
</head>
<body>
<div class="book"
data-level="0"
data-chapter-title="Introduction"
data-filepath="README.md"
data-basepath="."
data-revision="Fri Nov 30 2018 16:57:29 GMT+0100 (CET)"
data-innerlanguage="">
<div class="book-summary">
<nav role="navigation">
<ul class="summary">
<li class="chapter active" data-level="0" data-path="index.html">
<a href="./index.html">
<i class="fa fa-check"></i>
Introduction
</a>
</li>
<li class="chapter " data-level="1" >
<span><b>1.</b> Introduction to MobX</span>
<ul class="articles">
<li class="chapter " data-level="1.1" data-path="intro/overview.html">
<a href="./intro/overview.html">
<i class="fa fa-check"></i>
<b>1.1.</b>
The Gist of MobX
</a>
</li>
<li class="chapter " data-level="1.2" data-path="intro/concepts.html">
<a href="./intro/concepts.html">
<i class="fa fa-check"></i>
<b>1.2.</b>
Concepts & Principles
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="2" data-path="refguide/api.html">
<a href="./refguide/api.html">
<i class="fa fa-check"></i>
<b>2.</b>
API overview
</a>
</li>
<li class="chapter " data-level="3" >
<span><b>3.</b> Making things observable</span>
<ul class="articles">
<li class="chapter " data-level="3.1" data-path="refguide/observable.html">
<a href="./refguide/observable.html">
<i class="fa fa-check"></i>
<b>3.1.</b>
observable
</a>
</li>
<li class="chapter " data-level="3.2" data-path="refguide/observable-decorator.html">
<a href="./refguide/observable-decorator.html">
<i class="fa fa-check"></i>
<b>3.2.</b>
@observable
</a>
</li>
<li class="chapter " data-level="3.3" data-path="refguide/object.html">
<a href="./refguide/object.html">
<i class="fa fa-check"></i>
<b>3.3.</b>
objects
</a>
</li>
<li class="chapter " data-level="3.4" data-path="refguide/array.html">
<a href="./refguide/array.html">
<i class="fa fa-check"></i>
<b>3.4.</b>
arrays
</a>
</li>
<li class="chapter " data-level="3.5" data-path="refguide/map.html">
<a href="./refguide/map.html">
<i class="fa fa-check"></i>
<b>3.5.</b>
maps
</a>
</li>
<li class="chapter " data-level="3.6" data-path="refguide/boxed.html">
<a href="./refguide/boxed.html">
<i class="fa fa-check"></i>
<b>3.6.</b>
boxed values
</a>
</li>
<li class="chapter " data-level="3.7" data-path="refguide/modifiers.html">
<a href="./refguide/modifiers.html">
<i class="fa fa-check"></i>
<b>3.7.</b>
decorators
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="4" >
<span><b>4.</b> Reacting to observables</span>
<ul class="articles">
<li class="chapter " data-level="4.1" data-path="refguide/computed-decorator.html">
<a href="./refguide/computed-decorator.html">
<i class="fa fa-check"></i>
<b>4.1.</b>
(@)computed
</a>
</li>
<li class="chapter " data-level="4.2" data-path="refguide/autorun.html">
<a href="./refguide/autorun.html">
<i class="fa fa-check"></i>
<b>4.2.</b>
autorun
</a>
</li>
<li class="chapter " data-level="4.3" data-path="refguide/when.html">
<a href="./refguide/when.html">
<i class="fa fa-check"></i>
<b>4.3.</b>
when
</a>
</li>
<li class="chapter " data-level="4.4" data-path="refguide/reaction.html">
<a href="./refguide/reaction.html">
<i class="fa fa-check"></i>
<b>4.4.</b>
reaction
</a>
</li>
<li class="chapter " data-level="4.5" data-path="refguide/observer-component.html">
<a href="./refguide/observer-component.html">
<i class="fa fa-check"></i>
<b>4.5.</b>
(@)observer
</a>
</li>
<li class="chapter " data-level="4.6" data-path="best/react.html">
<a href="./best/react.html">
<i class="fa fa-check"></i>
<b>4.6.</b>
Understanding what MobX reacts to
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="5" >
<span><b>5.</b> Changing observables</span>
<ul class="articles">
<li class="chapter " data-level="5.1" data-path="refguide/action.html">
<a href="./refguide/action.html">
<i class="fa fa-check"></i>
<b>5.1.</b>
action
</a>
</li>
<li class="chapter " data-level="5.2" data-path="best/actions.html">
<a href="./best/actions.html">
<i class="fa fa-check"></i>
<b>5.2.</b>
async actions & flows
</a>
</li>
<li class="chapter " data-level="5.3" data-path="refguide/object-api.html">
<a href="./refguide/object-api.html">
<i class="fa fa-check"></i>
<b>5.3.</b>
Object api
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="6" >
<span><b>6.</b> Utility functions</span>
<ul class="articles">
<li class="chapter " data-level="6.1" data-path="refguide/tojson.html">
<a href="./refguide/tojson.html">
<i class="fa fa-check"></i>
<b>6.1.</b>
toJS
</a>
</li>
<li class="chapter " data-level="6.2" data-path="refguide/extend-observable.html">
<a href="./refguide/extend-observable.html">
<i class="fa fa-check"></i>
<b>6.2.</b>
extendObservable
</a>
</li>
<li class="chapter " data-level="6.3" data-path="refguide/extending.html">
<a href="./refguide/extending.html">
<i class="fa fa-check"></i>
<b>6.3.</b>
createAtom
</a>
</li>
<li class="chapter " data-level="6.4" data-path="refguide/observe.html">
<a href="./refguide/observe.html">
<i class="fa fa-check"></i>
<b>6.4.</b>
intercept & observe
</a>
</li>
<li class="chapter " data-level="6.5" >
<a target="_blank" href="https://github.com/mobxjs/mobx-utils#frompromise">
<i class="fa fa-check"></i>
<b>6.5.</b>
mobxUtils.fromPromise
</a>
</li>
<li class="chapter " data-level="6.6" >
<a target="_blank" href="https://github.com/mobxjs/mobx-utils#fromresource">
<i class="fa fa-check"></i>
<b>6.6.</b>
mobxUtils.fromResource
</a>
</li>
<li class="chapter " data-level="6.7" >
<a target="_blank" href="https://github.com/mobxjs/mobx-utils#tostream">
<i class="fa fa-check"></i>
<b>6.7.</b>
mobxUtils.toStream
</a>
</li>
<li class="chapter " data-level="6.8" >
<a target="_blank" href="https://github.com/mobxjs/mobx-utils#fromstream">
<i class="fa fa-check"></i>
<b>6.8.</b>
mobxUtils.fromStream
</a>
</li>
<li class="chapter " data-level="6.9" >
<a target="_blank" href="https://github.com/mobxjs/mobx-utils#now">
<i class="fa fa-check"></i>
<b>6.9.</b>
mobxUtils.now
</a>
</li>
<li class="chapter " data-level="6.10" data-path="refguide/create-transformer.html">
<a href="./refguide/create-transformer.html">
<i class="fa fa-check"></i>
<b>6.10.</b>
mobxUtils.createTransformer
</a>
</li>
<li class="chapter " data-level="6.11" data-path="refguide/expr.html">
<a href="./refguide/expr.html">
<i class="fa fa-check"></i>
<b>6.11.</b>
mobxUtils.expr
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="7" >
<a target="_blank" href="https://github.com/mobxjs/awesome-mobx#examples">
<i class="fa fa-check"></i>
<b>7.</b>
Blogs, videos, related projects
</a>
</li>
<li class="chapter " data-level="8" >
<span><b>8.</b> Tips & Tricks</span>
<ul class="articles">
<li class="chapter " data-level="8.1" data-path="faq/faq.html">
<a href="./faq/faq.html">
<i class="fa fa-check"></i>
<b>8.1.</b>
Frequently Asked Questions
</a>
</li>
<li class="chapter " data-level="8.2" data-path="best/pitfalls.html">
<a href="./best/pitfalls.html">
<i class="fa fa-check"></i>
<b>8.2.</b>
Common Pitfalls & Best Practices
</a>
</li>
<li class="chapter " data-level="8.3" data-path="best/decorators.html">
<a href="./best/decorators.html">
<i class="fa fa-check"></i>
<b>8.3.</b>
How to (not) use decorator syntax
</a>
</li>
<li class="chapter " data-level="8.4" data-path="best/trace.html">
<a href="./best/trace.html">
<i class="fa fa-check"></i>
<b>8.4.</b>
Using trace for debugging
</a>
</li>
<li class="chapter " data-level="8.5" data-path="best/store.html">
<a href="./best/store.html">
<i class="fa fa-check"></i>
<b>8.5.</b>
Defining data stores
</a>
</li>
<li class="chapter " data-level="8.6" data-path="best/react-performance.html">
<a href="./best/react-performance.html">
<i class="fa fa-check"></i>
<b>8.6.</b>
Optimizing React components
</a>
</li>
<li class="chapter " data-level="8.7" data-path="best/devtools.html">
<a href="./best/devtools.html">
<i class="fa fa-check"></i>
<b>8.7.</b>
DevTools
</a>
</li>
<li class="chapter " data-level="8.8" data-path="refguide/spy.html">
<a href="./refguide/spy.html">
<i class="fa fa-check"></i>
<b>8.8.</b>
spy
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="9" data-path="donating.html">
<a href="./donating.html">
<i class="fa fa-check"></i>
<b>9.</b>
Donate
</a>
</li>
<li class="divider"></li>
<li>
<a href="https://www.gitbook.com" target="blank" class="gitbook-link">
Published with GitBook
</a>
</li>
</ul>
</nav>
</div>
<div class="book-body">
<div class="body-inner">
<div class="book-header" role="navigation">
<!-- Actions Left -->
<!-- Title -->
<h1>
<i class="fa fa-circle-o-notch fa-spin"></i>
<a href="./" >MobX</a>
</h1>
</div>
<div class="page-wrapper" tabindex="-1" role="main">
<div class="page-inner">
<section class="normal" id="section-">
<html><head></head><body><a id="edit-link" href="https://github.com/mobxjs/mobx/tree/gh-pages/docs/README.md" class="btn fa fa-edit pull-left">  Edit This Page</a><p><img src="docs/mobx.png" alt="logo" height="120" align="right"></p>
<h1 id="mobx"><a name="mobx" class="plugin-anchor" href="#mobx"><span class="fa fa-link"></span></a>MobX</h1>
<p><em>Simple, scalable state management</em></p>
<p><a href="https://travis-ci.org/mobxjs/mobx" target="_blank"><img src="https://travis-ci.org/mobxjs/mobx.svg?branch=master" alt="Build Status"></a>
<a href="https://coveralls.io/github/mobxjs/mobx?branch=master" target="_blank"><img src="https://coveralls.io/repos/mobxjs/mobx/badge.svg?branch=master&service=github" alt="Coverage Status"></a>
<a href="https://gitter.im/mobxjs/mobx?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge" target="_blank"><img src="https://badges.gitter.im/Join%20Chat.svg" alt="Join the chat at https://gitter.im/mobxjs/mobx"></a>
<a href="https://hashnode.com/n/mobx" target="_blank"><img src="https://hashnode.github.io/badges/mobx.svg" alt="Discuss MobX on Hashnode"></a>
<a href="#backers"><img src="https://opencollective.com/mobx/backers/badge.svg" alt="OpenCollective"></a>
<a href="#sponsors"><img src="https://opencollective.com/mobx/sponsors/badge.svg" alt="OpenCollective"></a>
<a href="https://github.com/prettier/prettier" target="_blank"><img src="https://img.shields.io/badge/code_style-prettier-ff69b4.svg" alt="code style: prettier"></a></p>
<p>MobX is proudly sponsored by Mendix, Coinbase, Canva, Facebook Open Source, Algolia, Guilded and many <a href="#backers">individual sponsors</a></p>
<p><img src="docs/mendix-logo.png" align="center" width="100" title="Mendix" alt="Mendix">
<img src="docs/coinbase.jpeg" align="center" width="100" title="Coinbase" alt="Coinbase">
<img src="docs/canva.png" align="center" width="100" title="Canva" alt="Canva">
<img src="docs/fbos.jpeg" align="center" width="100" title="Facebook Open Source" alt="Facebook Open Source">
<img src="docs/algolia.jpg" align="center" width="100" title="Algolia" alt="Algolia">
<img src="docs/guilded.jpg" align="center" width="100" title="Guilded" alt="Guilded"></p>
<h1 id="installation"><a name="installation" class="plugin-anchor" href="#installation"><span class="fa fa-link"></span></a>Installation</h1>
<ul>
<li>Installation: <code>npm install mobx --save</code>. React bindings: <code>npm install mobx-react --save</code>. To enable ESNext decorators (optional), see below.</li>
<li>CDN:</li>
<li><a href="https://unpkg.com/mobx/lib/mobx.umd.js" target="_blank">https://unpkg.com/mobx/lib/mobx.umd.js</a></li>
<li><a href="https://cdnjs.com/libraries/mobx" target="_blank">https://cdnjs.com/libraries/mobx</a></li>
</ul>
<h1 id="browser-support"><a name="browser-support" class="plugin-anchor" href="#browser-support"><span class="fa fa-link"></span></a>Browser support</h1>
<ul>
<li>MobX >=5 runs on any browser with <a href="https://kangax.github.io/compat-table/es6/#test-Proxy" target="_blank">ES6 proxy support</a>. It will throw an error on startup on older environments such as IE11, Node.js <6 or React Native Android on old JavaScriptCore <a href="https://github.com/react-community/jsc-android-buildscripts#how-to-use-it-with-my-react-native-app" target="_blank">how-to-upgrade</a>.</li>
<li>MobX 4 runs on any ES5 browser and will be actively maintained. The MobX 4 and 5 api's are the same and semantically can achieve the same, but MobX 4 has some <a href="#mobx-4-vs-mobx-5">limitations</a>.</li>
</ul>
<p><em>Tip: the main entry point of the MobX 5 package ships with ES5 code for backward compatibility with all build tools. But since MobX 5 runs only on modern browsers anyway, consider using the faster and smaller ES6 build: <code>lib/mobx.es6.js</code>. For example by setting up a webpack alias: <code>resolve: { alias: { mobx: __dirname + "/node_modules/mobx/lib/mobx.es6.js" }}</code></em></p>
<h2 id="translations"><a name="translations" class="plugin-anchor" href="#translations"><span class="fa fa-link"></span></a>Translations</h2>
<ul>
<li><a href="http://cn.mobx.js.org" target="_blank">中文</a></li>
</ul>
<h2 id="getting-started"><a name="getting-started" class="plugin-anchor" href="#getting-started"><span class="fa fa-link"></span></a>Getting started</h2>
<ul>
<li><i><a style="color: white; background:green;padding:5px;margin:5px;border-radius:2px" href="https://egghead.io/courses/manage-complex-state-in-react-apps-with-mobx" target="_blank">egghead.io course</a></i></li>
<li><a href="https://mobxjs.github.io/mobx/getting-started.html" target="_blank">Ten minute, interactive MobX + React tutorial</a></li>
<li><img src="images/book.jpg" height="80px"> <a href="https://books.google.nl/books?id=ALFmDwAAQBAJ&pg=PP1&lpg=PP1&dq=michel+weststrate+mobx+quick+start+guide:+supercharge+the+client+state+in+your+react+apps+with+mobx&source=bl&ots=D460fxti0F&sig=ivDGTxsPNwlOjLHrpKF1nweZFl8&hl=nl&sa=X&ved=2ahUKEwiwl8XO--ncAhWPmbQKHWOYBqIQ6AEwAnoECAkQAQ#v=onepage&q=michel%20weststrate%20mobx%20quick%20start%20guide%3A%20supercharge%20the%20client%20state%20in%20your%20react%20apps%20with%20mobx&f=false" target="_blank">The MobX book</a> by Pavan Podila and Michel Weststrate (which despite it's name is in-depth!)</li>
<li><a href="https://mobxjs.github.io/mobx/refguide/api.html" target="_blank">Official MobX 4 documentation and API overview</a> (<a href="https://github.com/mobxjs/mobx/blob/54557dc319b04e92e31cb87427bef194ec1c549c/docs/refguide/api.md" target="_blank">MobX 3</a>, <a href="https://github.com/mobxjs/mobx/blob/7c9e7c86e0c6ead141bb0539d33143d0e1f576dd/docs/refguide/api.md" target="_blank">MobX 2</a>)</li>
<li>Videos:<ul>
<li><a href="https://www.youtube.com/watch?v=Aws40KOx90U" target="_blank">ReactNext 2016: Real World MobX</a> - 40m <a href="https://docs.google.com/presentation/d/1DrI6Hc2xIPTLBkfNH8YczOcPXQTOaCIcDESdyVfG_bE/edit?usp=sharing" target="_blank">slides</a></li>
<li><a href="https://www.youtube.com/watch?v=XGwuM_u7UeQ" target="_blank">Practical React with MobX</a>. In depth introduction and explanation to MobX and React by Matt Ruby on OpenSourceNorth (ES5 only) - 42m.</li>
<li>LearnCode.academy MobX tutorial <a href="https://www.youtube.com/watch?v=_q50BXqkAfI" target="_blank">Part I: MobX + React is AWESOME (7m)</a> <a href="https://www.youtube.com/watch?v=nYvNqKrl69s" target="_blank">Part II: Computed Values and Nested/Referenced Observables (12m.)</a></li>
<li><a href="https://www.youtube.com/watch?v=K8dr8BMU7-8" target="_blank">Screencast: intro to MobX</a> - 8m</li>
<li><a href="https://www.youtube.com/watch?v=ApmSsu3qnf0&feature=youtu.be" target="_blank">Talk: State Management Is Easy, React Amsterdam 2016 conf</a> (<a href="https://speakerdeck.com/mweststrate/state-management-is-easy-introduction-to-mobx" target="_blank">slides</a>)</li>
</ul>
</li>
<li><a href="http://mobxjs.github.io/mobx/faq/boilerplates.html" target="_blank">Boilerplates and related projects</a></li>
<li>More tutorials, blogs, videos, and other helpful resources can be found on the <a href="https://github.com/mobxjs/awesome-mobx#awesome-mobx" target="_blank">MobX awesome list</a></li>
</ul>
<h2 id="introduction"><a name="introduction" class="plugin-anchor" href="#introduction"><span class="fa fa-link"></span></a>Introduction</h2>
<p>MobX is a battle tested library that makes state management simple and scalable by transparently applying functional reactive programming (TFRP).
The philosophy behind MobX is very simple:</p>
<p><em>Anything that can be derived from the application state, should be derived. Automatically.</em></p>
<p>which includes the UI, data serialization, server communication, etc.</p>
<p><img alt="MobX unidirectional flow" src="docs/flow.png" align="center"></p>
<p>React and MobX together are a powerful combination. React renders the application state by providing mechanisms to translate it into a tree of renderable components. MobX provides the mechanism to store and update the application state that React then uses.</p>
<p>Both React and MobX provide an optimal and unique solutions to common problems in application development. React provides mechanisms to optimally render UI by using a virtual DOM that reduces the number of costly DOM mutations. MobX provides mechanisms to optimally synchronize application state with your React components by using a reactive virtual dependency state graph that is only updated when strictly needed and is never stale.</p>
<h2 id="core-concepts"><a name="core-concepts" class="plugin-anchor" href="#core-concepts"><span class="fa fa-link"></span></a>Core concepts</h2>
<p>MobX has only a few core concepts. The following snippets can be tried online using <a href="https://codesandbox.io/s/v3v0my2370" target="_blank">codesandbox example</a>.</p>
<h3 id="observable-state"><a name="observable-state" class="plugin-anchor" href="#observable-state"><span class="fa fa-link"></span></a>Observable state</h3>
<p><i><a style="color: white; background:green;padding:5px;margin:5px;border-radius:2px" href="https://egghead.io/lessons/javascript-sync-the-ui-with-the-app-state-using-mobx-observable-and-observer-in-react" target="_blank">Egghead.io lesson 1: observable & observer</a></i></p>
<p>MobX adds observable capabilities to existing data structures like objects, arrays and class instances.
This can simply be done by annotating your class properties with the <a href="http://mobxjs.github.io/mobx/refguide/observable-decorator.html" target="_blank">@observable</a> decorator (ES.Next).</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> { observable } <span class="hljs-keyword">from</span> <span class="hljs-string">"mobx"</span>
class Todo {
id = Math.random();
@observable title = <span class="hljs-string">""</span>;
@observable finished = <span class="hljs-literal">false</span>;
}
</code></pre>
<p>Using <code>observable</code> is like turning a property of an object into a spreadsheet cell.
But unlike spreadsheets, these values can be not only primitive values, but also references, objects and arrays.</p>
<p>If your environment doesn't support decorator syntax, don't worry.
You can read <a href="http://mobxjs.github.io/mobx/best/decorators.html" target="_blank">here</a> about how to set them up.
Or you can skip them altogether, as MobX can be used fine without decorator <em>syntax</em>, by leveraging the <em>decorate</em> utility.
Many MobX users do prefer the decorator syntax though, as it is slightly more concise.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> { decorate, observable } <span class="hljs-keyword">from</span> <span class="hljs-string">"mobx"</span>
class Todo {
id = Math.random();
title = <span class="hljs-string">""</span>;
finished = <span class="hljs-literal">false</span>;
}
decorate(Todo, {
title: observable,
finished: observable
})
</code></pre>
<h3 id="computed-values"><a name="computed-values" class="plugin-anchor" href="#computed-values"><span class="fa fa-link"></span></a>Computed values</h3>
<p><i><a style="color: white; background:green;padding:5px;margin:5px;border-radius:2px" href="https://egghead.io/lessons/javascript-derive-computed-values-and-manage-side-effects-with-mobx-reactions" target="_blank">Egghead.io lesson 3: computed values</a></i></p>
<p>With MobX you can define values that will be derived automatically when relevant data is modified.
By using the <a href="http://mobxjs.github.io/mobx/refguide/computed-decorator.html" target="_blank"><code>@computed</code></a> decorator or by using getter / setter functions when using <code>(extend)Observable</code> (Of course, you can use <code>decorate</code> here again as alternative to the <code>@</code> syntax).</p>
<pre><code class="lang-javascript"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">TodoList</span> </span>{
@observable todos = [];
@computed get unfinishedTodoCount() {
<span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>.todos.filter(todo => !todo.finished).length;
}
}
</code></pre>
<p>MobX will ensure that <code>unfinishedTodoCount</code> is updated automatically when a todo is added or when one of the <code>finished</code> properties is modified.
Computations like these resemble formulas in spreadsheet programs like MS Excel. They update automatically and only when required.</p>
<h3 id="reactions"><a name="reactions" class="plugin-anchor" href="#reactions"><span class="fa fa-link"></span></a>Reactions</h3>
<p><i><a style="color: white; background:green;padding:5px;margin:5px;border-radius:2px" href="https://egghead.io/lessons/react-write-custom-mobx-reactions-with-when-and-autorun" target="_blank">Egghead.io lesson 9: custom reactions</a></i></p>
<p>Reactions are similar to a computed value, but instead of producing a new value, a reaction produces a side effect for things like printing to the console, making network requests, incrementally updating the React component tree to patch the DOM, etc.
In short, reactions bridge <a href="https://en.wikipedia.org/wiki/Reactive_programming" target="_blank">reactive</a> and <a href="https://en.wikipedia.org/wiki/Imperative_programming" target="_blank">imperative</a> programming.</p>
<h4 id="react-components"><a name="react-components" class="plugin-anchor" href="#react-components"><span class="fa fa-link"></span></a>React components</h4>
<p><i><a style="color: white; background:green;padding:5px;margin:5px;border-radius:2px" href="https://egghead.io/courses/manage-complex-state-in-react-apps-with-mobx" target="_blank">Egghead.io lesson 1: observable & observer</a></i></p>
<p>If you are using React, you can turn your (stateless function) components into reactive components by simply adding the <a href="http://mobxjs.github.io/mobx/refguide/observer-component.html" target="_blank"><code>observer</code></a> function / decorator from the <code>mobx-react</code> package onto them.</p>
<pre><code class="lang-javascript">import React, {Component} from 'react';
import ReactDOM from 'react-dom';
import {observer} from 'mobx-react';
@observer
class TodoListView extends Component {
render() {
return <div>
<ul>
{this.props.todoList.todos.map(todo =>
<TodoView todo={todo} key={todo.id} />
)}
</ul>
Tasks left: {this.props.todoList.unfinishedTodoCount}
</div>
}
}
const TodoView = observer(({todo}) =>
<li>
<input
type="checkbox"
checked={todo.finished}
onClick={() => todo.finished = !todo.finished}
/>{todo.title}
</li>
)
const store = new TodoList();
ReactDOM.render(<TodoListView todoList={store} />, document.getElementById('mount'));
</code></pre>
<p><code>observer</code> turns React (function) components into derivations of the data they render.
When using MobX there are no smart or dumb components.
All components render smartly but are defined in a dumb manner. MobX will simply make sure the components are always re-rendered whenever needed, but also no more than that. So the <code>onClick</code> handler in the above example will force the proper <code>TodoView</code> to render, and it will cause the <code>TodoListView</code> to render if the number of unfinished tasks has changed.
However, if you would remove the <code>Tasks left</code> line (or put it into a separate component), the <code>TodoListView</code> will no longer re-render when ticking a box. You can verify this yourself by changing the <a href="https://jsfiddle.net/mweststrate/wv3yopo0/" target="_blank">JSFiddle</a>.</p>
<h4 id="custom-reactions"><a name="custom-reactions" class="plugin-anchor" href="#custom-reactions"><span class="fa fa-link"></span></a>Custom reactions</h4>
<p>Custom reactions can simply be created using the <a href="http://mobxjs.github.io/mobx/refguide/autorun.html" target="_blank"><code>autorun</code></a>,
<a href="http://mobxjs.github.io/mobx/refguide/reaction.html" target="_blank"><code>reaction</code></a> or <a href="http://mobxjs.github.io/mobx/refguide/when.html" target="_blank"><code>when</code></a> functions to fit your specific situations.</p>
<p>For example the following <code>autorun</code> prints a log message each time the amount of <code>unfinishedTodoCount</code> changes:</p>
<pre><code class="lang-javascript">autorun(() => {
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">"Tasks left: "</span> + todos.unfinishedTodoCount)
})
</code></pre>
<h3 id="what-will-mobx-react-to"><a name="what-will-mobx-react-to" class="plugin-anchor" href="#what-will-mobx-react-to"><span class="fa fa-link"></span></a>What will MobX react to?</h3>
<p>Why does a new message get printed each time the <code>unfinishedTodoCount</code> is changed? The answer is this rule of thumb:</p>
<p><em>MobX reacts to any existing observable property that is read during the execution of a tracked function.</em></p>
<p>For an in-depth explanation about how MobX determines to which observables needs to be reacted, check <a href="https://github.com/mobxjs/mobx/blob/gh-pages/docs/best/react.md" target="_blank">understanding what MobX reacts to</a>.</p>
<h3 id="actions"><a name="actions" class="plugin-anchor" href="#actions"><span class="fa fa-link"></span></a>Actions</h3>
<p><i><a style="color: white; background:green;padding:5px;margin:5px;border-radius:2px" href="https://egghead.io/lessons/react-use-mobx-actions-to-change-and-guard-state" target="_blank">Egghead.io lesson 5: actions</a></i></p>
<p>Unlike many flux frameworks, MobX is unopinionated about how user events should be handled.</p>
<ul>
<li>This can be done in a Flux like manner.</li>
<li>Or by processing events using RxJS.</li>
<li>Or by simply handling events in the most straightforward way possible, as demonstrated in the above <code>onClick</code> handler.</li>
</ul>
<p>In the end it all boils down to: Somehow the state should be updated.</p>
<p>After updating the state <code>MobX</code> will take care of the rest in an efficient, glitch-free manner. So simple statements, like below, are enough to automatically update the user interface.</p>
<p>There is no technical need for firing events, calling a dispatcher or what more. A React component in the end is nothing more than a fancy representation of your state. A derivation that will be managed by MobX.</p>
<pre><code class="lang-javascript">store.todos.push(
<span class="hljs-keyword">new</span> Todo(<span class="hljs-string">"Get Coffee"</span>),
<span class="hljs-keyword">new</span> Todo(<span class="hljs-string">"Write simpler code"</span>)
);
store.todos[<span class="hljs-number">0</span>].finished = <span class="hljs-literal">true</span>;
</code></pre>
<p>Nonetheless, MobX has an optional built-in concept of <a href="https://mobxjs.github.io/mobx/refguide/action.html" target="_blank"><code>actions</code></a>.
Read this section as well if you want to know more about writing asynchronous actions. It's easy!
Use them to your advantage; they will help you to structure your code better and make wise decisions about when and where state should be modified.</p>
<h2 id="mobx-simple-and-scalable"><a name="mobx-simple-and-scalable" class="plugin-anchor" href="#mobx-simple-and-scalable"><span class="fa fa-link"></span></a>MobX: Simple and scalable</h2>
<p>MobX is one of the least obtrusive libraries you can use for state management. That makes the <code>MobX</code> approach not just simple, but very scalable as well:</p>
<h3 id="using-classes-and-real-references"><a name="using-classes-and-real-references" class="plugin-anchor" href="#using-classes-and-real-references"><span class="fa fa-link"></span></a>Using classes and real references</h3>
<p>With MobX you don't need to normalize your data. This makes the library very suitable for very complex domain models (At Mendix for example ~500 different domain classes in a single application).</p>
<h3 id="referential-integrity-is-guaranteed"><a name="referential-integrity-is-guaranteed" class="plugin-anchor" href="#referential-integrity-is-guaranteed"><span class="fa fa-link"></span></a>Referential integrity is guaranteed</h3>
<p>Since data doesn't need to be normalized, and MobX automatically tracks the relations between state and derivations, you get referential integrity for free. Rendering something that is accessed through three levels of indirection?</p>
<p>No problem, MobX will track them and re-render whenever one of the references changes. As a result staleness bugs are a thing of the past. As a programmer you might forget that changing some data might influence a seemingly unrelated component in a corner case. MobX won't forget.</p>
<h3 id="simpler-actions-are-easier-to-maintain"><a name="simpler-actions-are-easier-to-maintain" class="plugin-anchor" href="#simpler-actions-are-easier-to-maintain"><span class="fa fa-link"></span></a>Simpler actions are easier to maintain</h3>
<p>As demonstrated above, modifying state when using MobX is very straightforward. You simply write down your intentions. MobX will take care of the rest.</p>
<h3 id="fine-grained-observability-is-efficient"><a name="fine-grained-observability-is-efficient" class="plugin-anchor" href="#fine-grained-observability-is-efficient"><span class="fa fa-link"></span></a>Fine grained observability is efficient</h3>
<p>MobX builds a graph of all the derivations in your application to find the least number of re-computations that is needed to prevent staleness. "Derive everything" might sound expensive, MobX builds a virtual derivation graph to minimize the number of recomputations needed to keep derivations in sync with the state.</p>
<p>In fact, when testing MobX at Mendix we found out that using this library to track the relations in our code is often a lot more efficient than pushing changes through our application by using handwritten events or "smart" selector based container components.</p>
<p>The simple reason is that MobX will establish far more fine grained 'listeners' on your data than you would do as a programmer.</p>
<p>Secondly MobX sees the causality between derivations so it can order them in such a way that no derivation has to run twice or introduces a glitch.</p>
<p>How that works? See this <a href="https://medium.com/@mweststrate/becoming-fully-reactive-an-in-depth-explanation-of-mobservable-55995262a254" target="_blank">in-depth explanation of MobX</a>.</p>
<h3 id="easy-interoperability"><a name="easy-interoperability" class="plugin-anchor" href="#easy-interoperability"><span class="fa fa-link"></span></a>Easy interoperability</h3>
<p>MobX works with plain javascript structures. Due to its unobtrusiveness it works with most javascript libraries out of the box, without needing MobX specific library flavors.</p>
<p>So you can simply keep using your existing router, data fetching, and utility libraries like <code>react-router</code>, <code>director</code>, <code>superagent</code>, <code>lodash</code> etc.</p>
<p>For the same reason you can use it out of the box both server and client side, in isomorphic applications and with react-native.</p>
<p>The result of this is that you often need to learn less new concepts when using MobX in comparison to other state management solutions.</p>
<hr>