Skip to content

Instantly share code, notes, and snippets.

@lucas-lm
Last active March 23, 2020 22:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lucas-lm/5560b862221221b21431addf87a63b92 to your computer and use it in GitHub Desktop.
Save lucas-lm/5560b862221221b21431addf87a63b92 to your computer and use it in GitHub Desktop.
Custom react hook for creating custom (fake) events
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