forked from mobxjs/mobx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.html
More file actions
1388 lines (896 loc) · 69.6 KB
/
Copy pathapi.html
File metadata and controls
1388 lines (896 loc) · 69.6 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>API overview | MobX</title>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<meta name="description" content="">
<meta name="generator" content="GitBook 2.4.3">
<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="next" href="../refguide/observable.html" />
<link rel="prev" href="../intro/concepts.html" />
</head>
<body>
<div class="book" data-level="2" data-basepath=".." data-revision="Sat Mar 10 2018 21:53:24 GMT+0100 (CET)">
<div class="book-summary">
<div class="book-search" role="search">
<input type="text" placeholder="Type to search" class="form-control" />
</div>
<nav role="navigation">
<ul class="summary">
<li class="chapter " 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 active" 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>
mobxUtis.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" >
<a target="_blank" href="https:/github.com/mobxjs/mobx-utils#keepalive">
<i class="fa fa-check"></i>
<b>6.10.</b>
mobxUtils.keepAlive
</a>
</li>
<li class="chapter " data-level="6.11" data-path="refguide/create-transformer.html">
<a href="../refguide/create-transformer.html">
<i class="fa fa-check"></i>
<b>6.11.</b>
mobxUtils.createTransformer
</a>
</li>
<li class="chapter " data-level="6.12" data-path="refguide/expr.html">
<a href="../refguide/expr.html">
<i class="fa fa-check"></i>
<b>6.12.</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 -->
<a href="#" class="btn pull-left toggle-summary" aria-label="Table of Contents"><i class="fa fa-align-justify"></i></a>
<a href="#" class="btn pull-left toggle-search" aria-label="Search"><i class="fa fa-search"></i></a>
<div id="font-settings-wrapper" class="dropdown pull-left">
<a href="#" class="btn toggle-dropdown" aria-label="Font Settings"><i class="fa fa-font"></i>
</a>
<div class="dropdown-menu font-settings">
<div class="dropdown-caret">
<span class="caret-outer"></span>
<span class="caret-inner"></span>
</div>
<div class="buttons">
<button type="button" id="reduce-font-size" class="button size-2">A</button>
<button type="button" id="enlarge-font-size" class="button size-2">A</button>
</div>
<div class="buttons font-family-list">
<button type="button" data-font="0" class="button">Serif</button>
<button type="button" data-font="1" class="button">Sans</button>
</div>
<div class="buttons color-theme-list">
<button type="button" id="color-theme-preview-0" class="button size-3" data-theme="0">White</button>
<button type="button" id="color-theme-preview-1" class="button size-3" data-theme="1">Sepia</button>
<button type="button" id="color-theme-preview-2" class="button size-3" data-theme="2">Night</button>
</div>
</div>
</div>
<!-- Actions Right -->
<div class="dropdown pull-right">
<a href="#" class="btn toggle-dropdown" aria-label="Share"><i class="fa fa-share-alt"></i>
</a>
<div class="dropdown-menu font-settings dropdown-left">
<div class="dropdown-caret">
<span class="caret-outer"></span>
<span class="caret-inner"></span>
</div>
<div class="buttons">
<button type="button" data-sharing="twitter" class="button">
Share on Twitter
</button>
<button type="button" data-sharing="google-plus" class="button">
Share on Google
</button>
<button type="button" data-sharing="facebook" class="button">
Share on Facebook
</button>
<button type="button" data-sharing="weibo" class="button">
Share on Weibo
</button>
<button type="button" data-sharing="instapaper" class="button">
Share on Instapaper
</button>
</div>
</div>
</div>
<a href="#" target="_blank" class="btn pull-right google-plus-sharing-link sharing-link" data-sharing="google-plus" aria-label="Google"><i class="fa fa-google-plus"></i></a>
<a href="#" target="_blank" class="btn pull-right facebook-sharing-link sharing-link" data-sharing="facebook" aria-label="Facebook"><i class="fa fa-facebook"></i></a>
<a href="#" target="_blank" class="btn pull-right twitter-sharing-link sharing-link" data-sharing="twitter" aria-label="Twitter"><i class="fa fa-twitter"></i></a>
<!-- 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/refguide/api.md" class="btn fa fa-edit pull-left">  Edit This Page</a><h1 id="mobx-api-reference"><a name="mobx-api-reference" class="plugin-anchor" href="#mobx-api-reference"><span class="fa fa-link"></span></a>MobX Api Reference</h1>
<p><strong>Applies to MobX 4 and higher</strong></p>
<ul>
<li>Using Mobx 3? Use this <a href="https://github.com/mobxjs/mobx/wiki/Migrating-from-mobx-3-to-mobx-4" target="_blank">migration guide</a> to switch gears.</li>
<li><a href="https://github.com/mobxjs/mobx/blob/54557dc319b04e92e31cb87427bef194ec1c549c/docs/refguide/api.md" target="_blank">MobX 3 documentation</a></li>
<li>For MobX 2, the old documentation is still available on <a href="https://github.com/mobxjs/mobx/blob/7c9e7c86e0c6ead141bb0539d33143d0e1f576dd/docs/refguide/api.md" target="_blank">github</a>.</li>
</ul>
<h1 id="core-api"><a name="core-api" class="plugin-anchor" href="#core-api"><span class="fa fa-link"></span></a>Core API</h1>
<p><em>These are the most important MobX API's.</em></p>
<blockquote>
<p>Understanding <code>observable</code>, <code>computed</code>, <code>reaction</code> and <code>action</code> is enough
to master MobX and use it in your applications!</p>
</blockquote>
<h2 id="creating-observables"><a name="creating-observables" class="plugin-anchor" href="#creating-observables"><span class="fa fa-link"></span></a>Creating observables</h2>
<h3 id="-observable-value"><a name="-observable-value" class="plugin-anchor" href="#-observable-value"><span class="fa fa-link"></span></a><code>observable(value)</code></h3>
<p>Usage:</p>
<ul>
<li><code>observable(value)</code></li>
<li><code>@observable classProperty = value</code></li>
</ul>
<p>Observable values can be JS primitives, references, plain objects, class instances, arrays and maps.</p>
<p><strong>Note:</strong> <code>observable(value)</code> is a convenience API that will succeed only if it can be made into
an observable data structure (<em>Array</em>, <em>Map</em>, or <em>observable-object</em>). For all other values, no conversion will be performed.</p>
<p>You can also directly create the desired observable type, see below.</p>
<p>The following conversion rules are applied, but can be fine-tuned by using <a href="#decorators"><em>decorators</em></a>. See below.</p>
<ol>
<li>If <em>value</em> is an instance of an <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map" target="_blank">ES6 Map</a>: a new <a href="map.html">Observable Map</a> will be returned. Observable maps are very useful if you don't want to react just to the change of a specific entry, but also to the addition or removal of entries.</li>
<li>If <em>value</em> is an array, a new <a href="array.html">Observable Array</a> will be returned.</li>
<li>If <em>value</em> is an object <em>without</em> prototype or its prototype is <code>Object.prototype</code>, the object will be cloned and all its current properties will be made observable. See <a href="object.html">Observable Object</a></li>
<li>If <em>value</em> is an object <em>with</em> a prototype, a JavaScript primitive or function, there will be no change made to the value. If you do need a <a href="boxed.html">Boxed Observable</a>, you can do one of the following:<ul>
<li>Call <code>observable.box(value)</code> explicitly</li>
<li>Use <code>@observable</code> in the class definition</li>
<li>Call <a href="#decorate"><code>decorate()</code></a></li>
<li>Use <code>extendObservable()</code> to introduce properties on a class definition</li>
</ul>
</li>
</ol>
<p>MobX will not make objects with a prototype automatically observable; as that is the responsibility of its constructor function. Use <code>extendObservable</code> in the constructor, or <code>@observable</code> in its class definition instead.</p>
<p>These rules might seem complicated at first sight, but you will notice that in practice they are very intuitive to work with.</p>
<p><strong>Some notes:</strong></p>
<ul>
<li>To create dynamically keyed objects, always use maps! Only initially existing properties on an object will be made observable, although new ones can be added using <code>extendObservable</code>.</li>
<li>To use the <code>@observable</code> decorator, make sure that <a href="http://mobxjs.github.io/mobx/refguide/observable-decorator.html" target="_blank">decorators are enabled</a> in your transpiler (babel or typescript).</li>
<li>By default making a data structure observable is <em>infective</em>; that means that <code>observable</code> is applied automatically to any value that is contained by the data structure, or will be contained by the data structure in the future. This behavior can be changed by using <a href="#decorators"><em>decorators</em></a>.</li>
</ul>
<p><a href="observable.html">«<code>observable</code>»</a> — <a href="observable-decorator.html">«<code>@observable</code>»</a></p>
<h3 id="-observable-property-value"><a name="-observable-property-value" class="plugin-anchor" href="#-observable-property-value"><span class="fa fa-link"></span></a><code>@observable property = value</code></h3>
<p><code>observable</code> can also be used as property decorator. It requires <a href="../best/decorators.html">decorators to be enabled</a> and is syntactic
sugar for <code>extendObservable(this, { property: value })</code>.</p>
<p><a href="observable-decorator.html">«<code>details</code>»</a></p>
<h3 id="-observable-box-value-options"><a name="-observable-box-value-options" class="plugin-anchor" href="#-observable-box-value-options"><span class="fa fa-link"></span></a><code>observable.box(value, options?)</code></h3>
<p>Creates an observable <em>box</em> that stores an observable reference to a value. Use <code>get()</code> to get the current value of the box, and <code>set()</code> to update it.
This is the foundation on which all other observables are built, but in practice you will use it rarely.</p>
<p>Normal boxes will automatically try to turn any new value into an observable if it isn't already. Use the <code>{deep: false}</code> option to disable this behavior.</p>
<p><a href="boxed.html">«<code>details</code>»</a></p>
<h3 id="-observable-object-value-decorators-options"><a name="-observable-object-value-decorators-options" class="plugin-anchor" href="#-observable-object-value-decorators-options"><span class="fa fa-link"></span></a><code>observable.object(value, decorators?, options?)</code></h3>
<p>Creates a clone of the provided object and makes all its properties observable.
By default any values in those properties will be made observable as well, but when using the <code>{deep: false}</code> options, only the properties will be made into observable
references, leaving the values untouched. (This holds also for any values assigned in the future).</p>
<p>The second argument in <code>observable.object()</code> can be used to fine tune the observability with <a href="#decorators"><code>decorators</code></a>.</p>
<p><a href="object.html">«<code>details</code>»</a>the</p>
<h3 id="-observable-array-value-options"><a name="-observable-array-value-options" class="plugin-anchor" href="#-observable-array-value-options"><span class="fa fa-link"></span></a><code>observable.array(value, options?)</code></h3>
<p>Creates a new observable array based on the provided value.</p>
<p>Use the <code>{deep: false}</code> option if the values in the array should not be turned into observables.</p>
<p><a href="array.html">«<code>details</code>»</a></p>
<h3 id="-observable-map-value-options"><a name="-observable-map-value-options" class="plugin-anchor" href="#-observable-map-value-options"><span class="fa fa-link"></span></a><code>observable.map(value, options?)</code></h3>
<p>Creates a new observable map based on the provided value. Use the <code>{deep: false}</code> option if the values in the map should not be turned into observables.</p>
<p>Use <code>map</code> whenever you want to create a dynamically keyed collections and the addition / removal of keys needs to be observed.
Since this uses the full-blown <em>ES6 Map</em> internally, you are free to use any type for the key and <em>not limited</em> to string keys.</p>
<p><a href="map.html">«<code>details</code>»</a></p>
<h3 id="-extendobservable"><a name="-extendobservable" class="plugin-anchor" href="#-extendobservable"><span class="fa fa-link"></span></a><code>extendObservable</code></h3>
<p>Usage: <code>extendObservable(target, properties, decorators?, options?)</code>.</p>
<p>For each key/value pair in each <code>propertyMap</code> a (new) observable property will be introduced on the target object.
This can be used in constructor functions to introduce observable properties without using decorators.
If a value of the <code>propertyMap</code> is a getter function, a <em>computed</em> property will be introduced.</p>
<p>Use <code>extendObservable(target, props, decorators?, {deep: false})</code> if the new properties should not be infective (that is; newly assigned values should not be turned into observables automatically).
Note that <code>extendObservable</code> enhances existing objects, unlike <code>observable.object</code> which creates a new object.</p>
<p><a href="extend-observable.html">«details»</a></p>
<h3 id="decorators"><a name="decorators" class="plugin-anchor" href="#decorators"><span class="fa fa-link"></span></a>Decorators</h3>
<p>Use decorators to fine tune the observability of properties defined via <code>observable</code>, <code>extendObservable</code> and <code>observable.object</code>. They can also control the auto-conversion rules for specific properties.</p>
<p>The following decorators are available:</p>
<ul>
<li><strong><code>observable.deep</code></strong>: This is the default decorator, used by any observable. It converts any assigned, non-primitive value into an observable if it isn't one yet.</li>
<li><strong><code>observable.ref</code></strong>: Disables automatic observable conversion, just creates an observable reference instead.</li>
<li><strong><code>observable.shallow</code></strong>: Can only be used in combination with collections. Turns any assigned collection into an collection, which is shallowly observable (instead of deep). In other words; the values inside the collection won't become observables automatically.</li>
<li><strong><code>computed</code></strong>: Creates a derived property, see <a href="computed-decorator.html"><code>computed</code></a></li>
<li><strong><code>action</code></strong>: Creates an action, see <a href="action.html"><code>action</code></a></li>
<li><strong><code>action.bound</code></strong>: Creates a bound action, see <a href="action.html"><code>action</code></a></li>
</ul>
<p>You can apply these decorators using the <em>@decorator</em> syntax:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> {observable, action} <span class="hljs-keyword">from</span> <span class="hljs-string">'mobx'</span>;
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">TaskStore</span> </span>{
@observable.shallow tasks = []
@action addTask(task) { <span class="hljs-comment">/* ... */</span> }
}
</code></pre>
<p>Or by passing in property decorators via <code>observable.object</code> / <code>observable.extendObservable</code> or <a href="#decorate"><code>decorate()</code></a>.
Note that decorators always 'stick' to the property. So they will remain in effect even if a new value is assigned.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> {observable, action} <span class="hljs-keyword">from</span> <span class="hljs-string">'mobx'</span>;
<span class="hljs-keyword">const</span> taskStore = observable({
tasks: [],
addTask(task) { <span class="hljs-comment">/* ... */</span> }
}, {
tasks: observable.shallow,
addTask: action
})
</code></pre>
<p><a href="modifiers.html">«details»</a></p>
<h3 id="-decorate"><a name="-decorate" class="plugin-anchor" href="#-decorate"><span class="fa fa-link"></span></a><code>decorate</code></h3>
<p>Usage: <code>decorate(object, decorators)</code>
This is a convenience method to apply observability <a href="#decorators">decorators</a> to the properties of a plain object or class instance. The second argument is an object with properties set to certain decorators.</p>
<p>Use this if you cannot use the <em>@decorator</em> syntax or need more control over setting observability.</p>
<pre><code class="lang-js"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">TodoList</span> </span>{
todos = {}
get unfinishedTodoCount() {
<span class="hljs-keyword">return</span> values(<span class="hljs-keyword">this</span>.todos).filter(todo => !todo.finished).length
}
addTodo() {