October 2019
Intermediate to advanced
426 pages
11h 49m
English
The useState Hook returns a value that will persist across re-renders, and a function to update it. A value for the initialState can be passed to it as an argument:
const [ state, setState ] = useState(initialState)
Calling setState updates the value and re-renders the component with the updated value. If the value did not change, React will not re-render the component.
A function can also be passed to the setState function, with the first argument being the current value. For example, consider the following code:
setState(val => val + 1)
Furthermore, a function can be passed to the first argument of the Hook if the initial state is the result of a complex computation. In that case, the function will only be called once ...
Read now
Unlock full access