-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyield.ts
47 lines (43 loc) · 1.17 KB
/
yield.ts
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
import * as React from 'react';
import createCache from './cache';
import { clearUndef } from './utils';
export default YieldComp => C => {
if (!YieldComp.name && !YieldComp.displayName) {
YieldComp.displayName = 'YieldComp';
}
if (!C) return YieldComp;
class Pure extends React.PureComponent {
render() {
return React.createElement(C, this.props);
}
}
return class Yield extends React.Component<any> {
state = { cache: createCache(), next: null };
static getDerivedStateFromProps(props, state) {
return {
next: Object.assign(
(extra, doCache) => {
if (!extra) return React.createElement(C, props);
return React.createElement(
Pure,
state.cache(
clearUndef({
...(typeof extra === 'function' ? extra(props) : extra),
next: props.next,
}),
doCache,
),
) as any;
},
{ noCache: true },
),
};
}
render() {
return React.createElement(YieldComp, {
...this.props,
next: this.state.next,
});
}
};
};