Guest User

Untitled

a guest
Apr 21st, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.96 KB | None | 0 0
  1. import React from 'react'
  2. import ReactDOM from 'react-dom'
  3. import PropTypes from 'react-props'
  4.  
  5. // Instead of using a HOC, we can share code using a
  6. // regular component with a render prop!
  7. class Mouse extends React.Component {
  8. static propTypes = {
  9. render: PropTypes.func.isRequired
  10. }
  11.  
  12. state = { x: 0, y: 0 }
  13.  
  14. handleMouseMove = (event) => {
  15. this.setState({
  16. x: event.clientX,
  17. y: event.clientY
  18. })
  19. }
  20.  
  21. render() {
  22. return (
  23. <div style={{ height: '100%' }} onMouseMove={this.handleMouseMove}>
  24. {this.props.render(this.state)}
  25. </div>
  26. )
  27. }
  28. }
  29.  
  30. const App = React.createClass({
  31. render() {
  32. return (
  33. <div style={{ height: '100%' }}>
  34. <Mouse render={({ x, y }) => (
  35. // The render prop gives us the state we need
  36. // to render whatever we want here.
  37. <h1>The mouse position is ({x}, {y})</h1>
  38. )}/>
  39. </div>
  40. )
  41. }
  42. })
  43.  
  44. ReactDOM.render(<App/>, document.getElementById('app'))
Add Comment
Please, Sign In to add comment