You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
*[Official documentation and API overview](https://mobxjs.github.io/mobx/refguide/api.html)
15
19
* Videos:
16
20
*[Egghead.io course: Manage Complex State in React Apps with MobX](https://egghead.io/courses/manage-complex-state-in-react-apps-with-mobx) - 30m.
17
21
*[Practical React with MobX](https://www.youtube.com/watch?v=XGwuM_u7UeQ). In depth introduction and explanation to MobX and React by Matt Ruby on OpenSourceNorth (ES5 only) - 42m.
18
22
* LearnCode.academy MobX tutorial [Part I: MobX + React is AWESOME (7m)](https://www.youtube.com/watch?v=_q50BXqkAfI)[Part II: Computed Values and Nested/Referenced Observables (12m.)](https://www.youtube.com/watch?v=nYvNqKrl69s)
19
23
*[Screencast: intro to MobX](https://www.youtube.com/watch?v=K8dr8BMU7-8) - 8m
20
-
*[State Management Is Easy, React Amsterdam 2016 conf](https://www.youtube.com/watch?v=ApmSsu3qnf0&feature=youtu.be) ([slides](https://speakerdeck.com/mweststrate/state-management-is-easy-introduction-to-mobx))
*[Transparent Reactive Programming and Mutable Data, Reactive2015 conf](https://www.youtube.com/watch?v=FEwLwiizlk0) ([slides](https://speakerdeck.com/mweststrate/react-transparent-reactive-programming-and-mutable-data-structures))
23
-
* More tutorials, blogs and videos can be found on the [MobX homepage](http://mobxjs.github.io/mobx/faq/blogs.html)
24
+
*[Talk: State Management Is Easy, React Amsterdam 2016 conf](https://www.youtube.com/watch?v=ApmSsu3qnf0&feature=youtu.be) ([slides](https://speakerdeck.com/mweststrate/state-management-is-easy-introduction-to-mobx))
24
25
*[Boilerplates and related projects](http://mobxjs.github.io/mobx/faq/boilerplates.html)
26
+
* More tutorials, blogs and videos can be found on the [MobX homepage](http://mobxjs.github.io/mobx/faq/blogs.html)
25
27
26
28
27
29
## Introduction
@@ -45,74 +47,45 @@ MobX has only a few core concepts. The following snippets can be tried online us
45
47
46
48
### Observable state
47
49
48
-
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 [@observable](http://mobxjs.github.io/mobx/refguide/observable-decorator.html) decorator (ES.Next), or by invoking the [`observable`](http://mobxjs.github.io/mobx/refguide/observable.html) or [`extendObservable`](http://mobxjs.github.io/mobx/refguide/extend-observable.html) functions (ES5). See [Language support](https://github.com/mobxjs/mobx/wiki/Language-Support) for language-specific examples.
50
+
MobX adds observable capabilities to existing data structures like objects, arrays and class instances.
51
+
This can simply be done by annotating your class properties with the [@observable](http://mobxjs.github.io/mobx/refguide/observable-decorator.html) decorator (ES.Next),
52
+
or by invoking the [`observable`](http://mobxjs.github.io/mobx/refguide/observable.html) or [`extendObservable`](http://mobxjs.github.io/mobx/refguide/extend-observable.html) functions (ES5).
53
+
Scroll down for more ES5 / ES6 and ES.next examples.
49
54
50
55
```javascript
51
-
// ESNext class example with decorators:
52
-
classTodo {
53
-
id =Math.random();
54
-
@observable title ="";
55
-
@observable finished =false;
56
-
}
57
-
58
-
// ES6 class without decorators:
59
-
classTodo {
60
-
constructor() {
61
-
this.id=Math.random()
62
-
extendObservable(this, {
63
-
title:"",
64
-
finished:false
65
-
})
66
-
}
67
-
}
68
-
69
-
// ES5 constructor function example:
70
-
functionTodo() {
71
-
this.id=Math.random()
72
-
extendObservable(this, {
73
-
title:"",
74
-
finished:false
75
-
})
76
-
}
77
-
78
-
// ... or just create plain objects:
79
56
functioncreateTodo() {
80
57
returnobservable({
81
58
id:Math.random(),
82
59
title:"",
83
60
finished:false
84
61
})
85
62
}
86
-
87
63
```
88
64
89
-
Using `@observable` is like turning a value into a spreadsheet cell. But unlike spreadsheets, not only can these values be primitive values, but references, objects and arrays as well. You can even [define your own](http://mobxjs.github.io/mobx/refguide/extending.html) observable data sources.
65
+
Using `observable` is like turning the properties of an object into a spreadsheet cells.
66
+
But unlike spreadsheets, these values cannot just be primitive values, but also references, objects and arrays.
67
+
You can even [define your own](http://mobxjs.github.io/mobx/refguide/extending.html) observable data sources.
90
68
91
69
### Computed values
92
70
93
-
With MobX you can define values that will be derived automatically when relevant data is modified. By using the [`@computed`](http://mobxjs.github.io/mobx/refguide/computed-decorator.html) decorator or by using parameterless functions as property values in `extendObservable`.
71
+
With MobX you can define values that will be derived automatically when relevant data is modified.
72
+
By using the [`@computed`](http://mobxjs.github.io/mobx/refguide/computed-decorator.html) decorator or by using getter / setter functions when using `(extend)Observable`.
MobX will ensure that `unfinishedTodoCount` is updated automatically when a todo is added or when one of the `finished` properties is modified.
117
90
Computations like these can very well be compared with formulas in spreadsheet programs like MS Excel. They update automatically whenever, and only when, needed.
118
91
@@ -121,16 +94,14 @@ Computations like these can very well be compared with formulas in spreadsheet p
121
94
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.
122
95
In short, reactions bridge [reactive](https://en.wikipedia.org/wiki/Reactive_programming) and [imperative](https://en.wikipedia.org/wiki/Imperative_programming) programming.
123
96
124
-
If you are using React, you can turn your (stateless function) components into reactive components by simply adding the [`@observer`](http://mobxjs.github.io/mobx/refguide/observer-component.html) decorator from the `mobx-react` package onto them.
97
+
If you are using React, you can turn your (stateless function) components into reactive components by simply adding the [`observer`](http://mobxjs.github.io/mobx/refguide/observer-component.html) function / decorator from the `mobx-react` package onto them.
var TodoListView =observer(React.createClass({ /* etc */ }))
163
-
```
164
-
165
131
`observer` turns React (function) components into derivations of the data they render.
166
-
167
-
Also, reactions can be created using the [`autorun`](http://mobxjs.github.io/mobx/refguide/autorun.html), [`autorunAsync`](http://mobxjs.github.io/mobx/refguide/autorun-async.html) or [`when`](http://mobxjs.github.io/mobx/refguide/when.html) functions to fit your specific situations.
168
-
169
132
When using MobX there are no smart or dumb components.
170
-
171
133
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 `onClick` handler in the above example will force the proper `TodoView` to render, and it will cause the `TodoListView` to render if the number of unfinished tasks has changed.
172
-
173
134
However, if you would remove the `Tasks left` line (or put it into a separate component), the `TodoListView` will no longer re-render when ticking a box. You can verify this yourself by changing the [JSFiddle](https://jsfiddle.net/mweststrate/wv3yopo0/).
174
135
136
+
Custom reactions can simply be created using the [`autorun`](http://mobxjs.github.io/mobx/refguide/autorun.html),
137
+
[`autorunAsync`](http://mobxjs.github.io/mobx/refguide/autorun-async.html) or [`when`](http://mobxjs.github.io/mobx/refguide/when.html) functions to fit your specific situations.
138
+
175
139
For an in-depth explanation about how MobX determines to which observables needs to be reacted, check out: [Understanding what MobX reacts to](https://github.com/mobxjs/mobx/blob/gh-pages/docs/best/react.md)
Finally, `@observer` can be used as decorator in ES.next / Typescript if decorators are enabled:
283
+
284
+
```javascript
285
+
@observer
286
+
classTimerextendsReact.Component {
287
+
/* ... */
288
+
}
289
+
```
290
+
291
+
### Enabling decorators (optional)
292
+
293
+
**TypeScript**
294
+
295
+
Enable the compiler option `experimentalDecorators` in `tsconfig.json` or pass it as flag `--experimentalDecorators` to the compiler.
296
+
297
+
**Babel:**
298
+
299
+
Install support for decorators: `npm i --save-dev babel-plugin-transform-decorators-legacy`. And enable it in your `.babelrc` file:
300
+
301
+
```
302
+
{
303
+
"presets": [
304
+
"es2015",
305
+
"stage-1"
306
+
],
307
+
"plugins": ["transform-decorators-legacy"]
308
+
}
309
+
```
310
+
311
+
Note that the order is important and `transform-decorators-legacy` should be listed *first*.
312
+
202
313
## MobX: Simple and scalable
203
314
204
315
MobX is one of the least obtrusive libraries you can use for state management. That makes the `MobX` approach not just simple, but very scalable as well:
@@ -283,27 +394,6 @@ And finally kudo's for all the people that believed in, tried, validated and eve
283
394
* Feel free to send small pull requests. Please discuss new features or big changes in a GitHub issue first.
284
395
* Use `npm test` to run the basic test suite, `npm run coverage` for the test suite with coverage and `npm run perf` for the performance tests.
285
396
286
-
## Enabling decorators (optional)
287
-
288
-
**TypeScript**
289
-
290
-
Enable the compiler option `experimentalDecorators` in `tsconfig.json` or pass it as flag `--experimentalDecorators` to the compiler.
291
-
292
-
**Babel:**
293
-
294
-
Install support for decorators: `npm i --save-dev babel-plugin-transform-decorators-legacy`. And enable it in your `babelrc` file:
295
-
296
-
```
297
-
{
298
-
"presets": [
299
-
"es2015",
300
-
"stage-1"
301
-
],
302
-
"plugins": ["transform-decorators-legacy"]
303
-
}
304
-
```
305
-
Probably you have more plugins and presets in your `.babelrc` already, note that the order is important and `transform-decorators-legacy` should come as first.
306
-
307
397
## Bower support
308
398
309
399
Bower support is available through the infamous npmcdn.com:
0 commit comments