-
-
Save kylpo/0ffd4d146b182601e171b71bcdaf9943 to your computer and use it in GitHub Desktop.
This file contains 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 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