-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathWaterflow.js
43 lines (34 loc) · 1.03 KB
/
Waterflow.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
import React from 'react';
import PropTypes from 'prop-types';
import memoizeState from 'memoize-state';
import { polyfill } from 'react-lifecycles-compat';
const genFlow = flow => flow.map(fn => memoizeState(fn));
export class MemoizedFlow extends React.Component {
static propTypes = {
/* eslint-disable */
input: PropTypes.object.isRequired,
/* eslint-enable */
flow: PropTypes.arrayOf(PropTypes.func).isRequired,
children: PropTypes.func.isRequired,
pure: PropTypes.bool,
};
static defaultProps = {
pure: false,
};
static getDerivedStateFromProps(props, state) {
return {
value: state.flow.reduce((value, fn) => Object.assign({}, value, fn(value)), props.input),
};
}
state = {
// eslint-disable-next-line react/no-unused-state
flow: genFlow(this.props.flow),
};
shouldComponentUpdate(nextProps, nextState) {
return !this.props.pure || nextState.value !== this.state.value;
}
render() {
return this.props.children(this.state.value);
}
}
polyfill(MemoizedFlow);