-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path02autorun.jsx
46 lines (37 loc) · 928 Bytes
/
02autorun.jsx
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
import React from 'react';
import ReactDOM from 'react-dom';
import { observable, computed, autorun } from 'mobx';
import { observer } from 'mobx-react';
let count = 0;
class Store {
@observable numbers = [1, 2, 3];
@observable showSum = true;
@computed get sum() {
count++;
return this.numbers.reduce((a, b) => a + b, 0);
}
}
const store = new Store();
window.store = store;
@observer
class Demo extends React.Component {
constructor(props) {
super(props);
this.store = this.props.store;
window.disposer = autorun(() => console.log(`numbers is ${store.numbers} count is ${count}`));
}
render() {
return (
<div>
<ul>
{this.store.numbers.map((x, i) => <li key={i}>{x}</li>)}
</ul>
{this.store.showSum && <b>sum is {this.store.sum}</b>}
</div>
)
}
}
ReactDOM.render(
<Demo store={store} />,
document.getElementById('root'),
);