Last active
March 23, 2020 22:12
-
-
Save lucas-lm/5560b862221221b21431addf87a63b92 to your computer and use it in GitHub Desktop.
Custom react hook for creating custom (fake) events
This file contains 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' | |
// TODO: implement payload state, where data from the fakeEvent will be stored if it has any | |
const useFakeEvent = (handler=()=>{}) => { | |
const [ fakeEventCount, setFakeEventCount ] = useState(0) // It 'fires' the fakeEvent and counts how many times the fakeEvent was fired | |
useEffect(() => { | |
if (fakeEventCount > 0) handler() // Conditional for ignoring component startup | |
}, [fakeEventCount, handler]) | |
const fakeEventEmitter = () => { | |
setFakeEventCount(c => c + 1) | |
} | |
return fakeEventEmitter | |
} | |
export default useFakeEvent |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment