Last active
November 11, 2017 19:08
-
-
Save eddyw/480847e59180de2de3d549eabb68dc8d to your computer and use it in GitHub Desktop.
Map Component
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MapArray extends React.Component { | |
static propTypes = { | |
from: propTypes.array.isRequired, | |
children: propTypes.element.isRequired, | |
map: propTypes.func, | |
} | |
static defaultProps = { | |
map: e => e, | |
} | |
shouldComponentUpdate(nextProps) { | |
return nextProps.from !== this.props.from | |
} | |
render() { | |
const { | |
from, | |
children, | |
map, | |
} = this.props | |
const child = React.Children.only(children) | |
const mapped = from.map((item, key) => ( | |
React.cloneElement(child, { | |
key, | |
...map(item, key) | |
}) | |
)) | |
return mapped | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment