Last active
December 6, 2021 15:34
-
-
Save Neo42/c8d5eccd32343220ba3c6d761d507158 to your computer and use it in GitHub Desktop.
useState lazy initialization by passing a callback function
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
const [name, setName] = React.useState( | |
someExpensiveComputation(props); // run on every render | |
) | |
const [state, setState] = useState(() => { | |
const initialState = someExpensiveComputation(props); // only run on the initial render | |
return initialState; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment