Created
December 13, 2019 17:37
-
-
Save kcoyner/994c081ca9634bbacadea7fea8a8f354 to your computer and use it in GitHub Desktop.
useState/useEffect vs useMemo - essentially equivalent
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
// useEffect & useState | |
const useCalculate = numberProp => { | |
const [result, setResult] = useState<number>(null); | |
useEffect(() => { | |
setResult(expensiveCalculation(numberProp)); | |
}, [numberProp]); | |
return result; | |
}; | |
// useMemo | |
const useCalculateWithMemo = numberProp => { | |
return useMemo(() => { | |
return expensiveCalculation(numberProp); | |
}, [numberProp]); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment