Last active
July 16, 2019 07:40
-
-
Save yingjieg/180fb2997e592508fa087f0ea3ba28d5 to your computer and use it in GitHub Desktop.
React
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
| export default function higherOrderComponent(WrappedComponent) { | |
| return class EnhancedComponent extends Component { | |
| render() { | |
| return <WrappedComponent {...this.props} {...this.state} />; | |
| } | |
| }; | |
| } | |
| const EnhancedComponent = higherOrderComponent(<DummyComponent />); |
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 { useState, useEffect } from 'react'; | |
| export function useMouseLocation() { | |
| const [mouseX, setMouseX] = useState(25); | |
| const [mouseY, setMouseY] = useState(25); | |
| function handler(event) { | |
| const { clientX, clientY } = event; | |
| setMouseX(clientX); | |
| setMouseY(clientY); | |
| } | |
| useEffect(() => { | |
| window.addEventListener('mousemove', handler); | |
| return () => { | |
| window.removeEventListener('mousemove', handler); | |
| }; | |
| }); | |
| return [mouseX, mouseY]; | |
| } |
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 { useMouseLocation } from 'mouse-location-hook'; | |
| export default function MouseTracker() { | |
| const [mouseX, mouseY] = useMouseLocation(); | |
| return ( | |
| <div> | |
| mouseX: {mouseX}, mouseY: {mouseY} | |
| </div> | |
| ); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment