Skip to content

Instantly share code, notes, and snippets.

@Pyrolistical
Created October 2, 2021 06:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Pyrolistical/cfb4840c8154fa06cb3c4ee9b0602ad3 to your computer and use it in GitHub Desktop.
Save Pyrolistical/cfb4840c8154fa06cb3c4ee9b0602ad3 to your computer and use it in GitHub Desktop.
https://blog.battlefy.com/how-to-escape-react-hooks-hell-a66c0d142c9e Unnecessary useEffect and useState for computed value based on prop
// BEFORE
const [costlyValue, setCostlyValue] = setState();
useEffect(() => {
setCostlyValue(computedCostlyValue(props.someParam));
}, [props.someParam]);
// AFTER
const costlyValue = useMemo(() => computedCostlyValue(props.someParam), [props.someParam]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment