Skip to content

Instantly share code, notes, and snippets.

@kylpo

kylpo/block1.jsx Secret

Created June 21, 2017 23:23
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 kylpo/0ffd4d146b182601e171b71bcdaf9943 to your computer and use it in GitHub Desktop.
Save kylpo/0ffd4d146b182601e171b71bcdaf9943 to your computer and use it in GitHub Desktop.
class Press_ extends React.Component {
render() {
// React.cloneElement() requires a single child
const Child = React.Children.only(this.props.children)
// compute props to pass
const propsToPass = {onPress: this.props.onPress}
// return new element with props to pass
// note: this could also be wrapped in other components, if we wanted
return React.cloneElement(Child, propsToPass)
}
}
class MyComponent extends React.Component {
handlePress = () => {
// ...
}
render() {
return (
<Press_ onPress={this.handlePress}>
<div />
</Press_>
)
}
}
ReactDom.Render(<MyComponent />, /* ... */)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment