Skip to content

Instantly share code, notes, and snippets.

@mattpocock
Created May 28, 2019 18:14
Show Gist options
  • Select an option

  • Save mattpocock/175d3e1a28d15e78dc7f9dc2923e6784 to your computer and use it in GitHub Desktop.

Select an option

Save mattpocock/175d3e1a28d15e78dc7f9dc2923e6784 to your computer and use it in GitHub Desktop.
import { useEffect, useState } from 'react'
const useWatchForTruthy = (value, callback) => {
const [prevValue, setPrevValue] = useState(null)
const [isFirstRun, setIsFirstRun] = useState(true)
useEffect(() => {
if (!isFirstRun && !prevValue && value) {
callback()
}
setPrevValue(value)
if (isFirstRun) {
setIsFirstRun(false)
}
}, [value])
}
export default useWatchForTruthy
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment