Created
May 28, 2019 18:14
-
-
Save mattpocock/175d3e1a28d15e78dc7f9dc2923e6784 to your computer and use it in GitHub Desktop.
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, 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