11////////////////////////////////////////////////////////////////////////////////
22// Exercise:
33//
4- // - Make `withMouse` a "higher-order component" that sends the mouse position
5- // to the component as props (hint: use `event.clientX` and `event.clientY`).
4+ // - Make the mouse-tracking logic reusable by filling in the `withMouse`
5+ // higher-order component and returning a new component that renders a
6+ // `ComposedComponent` element with the current mouse position as props
7+ // - Use the `withMouse` function you just wrote to create an `AppWithMouse`
8+ // component
9+ // - Render <AppWithMouse> instead of <App>
610//
711// Got extra time?
812//
@@ -14,35 +18,28 @@ import React from "react";
1418import ReactDOM from "react-dom" ;
1519import PropTypes from "prop-types" ;
1620
17- function withMouse ( Component ) {
18- return Component ;
21+ function withMouse ( ComposedComponent ) {
22+ // TODO
1923}
2024
2125class App extends React . Component {
22- static propTypes = {
23- mouse : PropTypes . shape ( {
24- x : PropTypes . number . isRequired ,
25- y : PropTypes . number . isRequired
26- } )
26+ state = { x : 0 , y : 0 } ;
27+
28+ handleMouseMove = event => {
29+ this . setState ( { x : event . clientX , y : event . clientY } ) ;
2730 } ;
2831
2932 render ( ) {
30- const { mouse } = this . props ;
33+ const { x , y } = this . state ;
3134
3235 return (
33- < div className = "container" >
34- { mouse ? (
35- < h1 >
36- The mouse position is ({ mouse . x } , { mouse . y } )
37- </ h1 >
38- ) : (
39- < h1 > We don't know the mouse position yet :(</ h1 >
40- ) }
36+ < div className = "container" onMouseMove = { this . handleMouseMove } >
37+ < h1 >
38+ The mouse position is ({ x } , { y } )
39+ </ h1 >
4140 </ div >
4241 ) ;
4342 }
4443}
4544
46- const AppWithMouse = withMouse ( App ) ;
47-
48- ReactDOM . render ( < AppWithMouse /> , document . getElementById ( "app" ) ) ;
45+ ReactDOM . render ( < App /> , document . getElementById ( "app" ) ) ;
0 commit comments