-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiterate.js
37 lines (34 loc) · 1.06 KB
/
iterate.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
import React, {Component, PropTypes, Children, createElement} from 'react';
import shallowEqual from 'shallowequal';
class Iterate extends Component {
constructor(...args) {
super(...args);
this.previousProps = null;
}
shouldComponentUpdate(nextProps) {
const {root, rootProps, items, children, ...props} = nextProps;
return children !== this.props.children ||
items !== this.props.items ||
root !== this.props.root ||
!shallowEqual(rootProps, this.props.rootProps) ||
!shallowEqual(props, this.previousProps);
}
render() {
const {root, rootProps, items, children, ...props} = this.props;
this.previousProps = props;
return createElement(root, {
...rootProps,
children: Children.toArray(items).map((item, index) => children(item, props, index))
});
}
}
Iterate.propTypes = {
children: PropTypes.func.isRequired,
items: PropTypes.array,
root: PropTypes.oneOfType([PropTypes.string, PropTypes.func]),
rootProps: PropTypes.object
};
Iterate.defaultProps = {
root: 'div'
};
export default Iterate;