/
component.js
88 lines (76 loc) · 2.3 KB
/
component.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
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
import { FORCE_RENDER } from './constants';
import { extend } from './util';
import { renderComponent } from './vdom/component';
import { enqueueRender } from './render-queue';
/**
* Base Component class.
* Provides `setState()` and `forceUpdate()`, which trigger rendering.
* @typedef {object} Component
* @param {object} props The initial component props
* @param {object} context The initial context from parent components' getChildContext
* @public
*
* @example
* class MyFoo extends Component {
* render(props, state) {
* return <div />;
* }
* }
*/
export function Component(props, context) {
this._dirty = true;
/**
* @public
* @type {object}
*/
this.context = context;
/**
* @public
* @type {object}
*/
this.props = props;
/**
* @public
* @type {object}
*/
this.state = this.state || {};
this._renderCallbacks = [];
}
extend(Component.prototype, {
/**
* Update component state and schedule a re-render.
* @param {object} state A dict of state properties to be shallowly merged
* into the current state, or a function that will produce such a dict. The
* function is called with the current state and props.
* @param {() => void} callback A function to be called once component state is
* updated
*/
setState(state, callback) {
const prev = this.prevState = this.state;
if (typeof state === 'function') state = state(prev, this.props);
this.state = extend(extend({}, prev), state);
if (callback) this._renderCallbacks.push(callback);
enqueueRender(this);
},
/**
* Immediately perform a synchronous re-render of the component.
* @param {() => void} callback A function to be called after component is
* re-rendered.
* @private
*/
forceUpdate(callback) {
if (callback) this._renderCallbacks.push(callback);
renderComponent(this, FORCE_RENDER);
},
/**
* Accepts `props` and `state`, and returns a new Virtual DOM tree to build.
* Virtual DOM is generally constructed via [JSX](http://jasonformat.com/wtf-is-jsx).
* @param {object} props Props (eg: JSX attributes) received from parent
* element/component
* @param {object} state The component's current state
* @param {object} context Context object, as returned by the nearest
* ancestor's `getChildContext()`
* @returns {import('./vnode').VNode | void}
*/
render() {}
});