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