Created
May 18, 2019 23:50
-
-
Save drewwyatt/4449b88203731696562d769f0023dc52 to your computer and use it in GitHub Desktop.
onClick={wat} snippet
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 { useEffect, createRef } from 'react'; | |
const stopPropagationAndUseHandler = (handler: EventListener): EventListener => e => { | |
e.stopImmediatePropagation(); | |
handler(e); | |
} | |
export const useNativeEvent = (type: string, handler: EventListener) => { | |
const ref: React.RefObject<any> = createRef(); | |
useEffect( | |
() => { | |
if (ref.current) { | |
ref.current.addEventListener(type, stopPropagationAndUseHandler(handler), true); | |
} | |
}, | |
[ref] | |
); | |
return ref; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment