The useState Hook

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 during the initialization of the Hook:

const [ state, setState ] = useState(() => {
return computeInitialState()
})

The State Hook is the most basic and ubiquitous Hook provided by React.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.223.124.244