-
Notifications
You must be signed in to change notification settings - Fork 2
/
kefir-react.es6
56 lines (48 loc) · 1.29 KB
/
kefir-react.es6
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
import React from 'react';
const T = React.PropTypes;
const h = React.createElement;
export class KefirReactComponent extends React.Component {
static propTypes = {
props$: T.any.isRequired, // Kefir.Observable
render: T.func.isRequired // props => component
};
state = {
values: null
};
_subscribe = props$ => {
const handler = values => {
this.setState({
values
});
}
props$.onValue(handler);
this._unsubscribe = () => {
props$.offValue(handler);
this._unsubscribe = null;
};
}
componentWillMount() {
this._subscribe(this.props.props$);
}
componentWillReceiveProps(nextProps) {
if (nextProps.props$ !== this.props.props$) {
this._unsubscribe();
this._subscribe(nextProps.props$);
}
}
componentWillUnmount() {
this._unsubscribe();
}
render() {
return this.props.render(this.state.values);
}
}
export const KefirReact = (props$, Component, Loader = Component) => {
return props => h(KefirReactComponent, {
props$,
render: values => values ? h(Component, {
...props,
...values
}) : h(Loader, props)
})
};