Guest User

Untitled

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