Skip to content

Instantly share code, notes, and snippets.

@eddyw
Last active November 11, 2017 19:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eddyw/480847e59180de2de3d549eabb68dc8d to your computer and use it in GitHub Desktop.
Save eddyw/480847e59180de2de3d549eabb68dc8d to your computer and use it in GitHub Desktop.
Map Component
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