forked from almin/almin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
45 lines (42 loc) · 1.3 KB
/
App.js
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
// LICENSE : MIT
"use strict";
import React from "react";
import PropTypes from "prop-types";
import { Context } from "almin";
import { CounterState } from "../store/CounterState";
import { Counter } from "./Counter";
export default class App extends React.Component {
componentWillMount() {
const appContext = this.props.appContext;
// set initial state
this.setState(appContext.getState());
// update component's state with store's state when store is changed
const onChangeHandler = () => {
this.setState(appContext.getState());
};
this.unSubscribe = appContext.onChange(onChangeHandler);
}
componentWillUnmount() {
if (typeof this.unSubscribe === "function") {
this.unSubscribe();
}
}
render() {
/**
* Where is "CounterState" come from?
* It is a `key` of StoreGroup.
*
* ```
* const storeGroup = new StoreGroup({
* "counter": counterStore
* });
* ```
* @type {CounterState}
*/
const counterState = this.state.counter;
return <Counter counterState={counterState} appContext={this.props.appContext} />;
}
}
App.propTypes = {
appContext: PropTypes.instanceOf(Context).isRequired
};