Last active
May 25, 2018 07:03
-
-
Save dferber90/701ff95e7f698a983dc23de6278482a8 to your computer and use it in GitHub Desktop.
Test a Render Prop
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
| import React from 'react'; | |
| import PropTypes from 'prop-types'; | |
| // Instead of using a HOC, we can share code using a | |
| // regular component with a render prop! | |
| class Mouse extends React.Component { | |
| static propTypes = { | |
| render: PropTypes.func.isRequired, | |
| }; | |
| state = { x: 0, y: 0 }; | |
| handleMouseMove = event => { | |
| this.setState({ | |
| x: event.clientX, | |
| y: event.clientY, | |
| }); | |
| }; | |
| render() { | |
| return ( | |
| <div style={{ height: '100%' }} onMouseMove={this.handleMouseMove}> | |
| {this.props.render(this.state)} | |
| </div> | |
| ); | |
| } | |
| } | |
| class App extends React.Component { | |
| render() { | |
| return ( | |
| <div style={{ height: '100%' }}> | |
| <Mouse | |
| render={({ x, y }) => ( | |
| // The render prop gives us the state we need | |
| // to render whatever we want here. | |
| <h1> | |
| The mouse position is ({x}, {y}) | |
| </h1> | |
| )} | |
| /> | |
| </div> | |
| ); | |
| } | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment