When our components are first rendered, they probably expect some state values to be set. This is called the initial state of the component, and we can use the useState() Hook to set the initial state. Let's take a look at an example:
import React, { Fragment, useState } from 'react';
export default function App() {
const [name] = useState('Adam');
const [age] = useState(35);
return (
<Fragment>
<p>My name is {name}</p>
<p>My age is {age}</p>
</Fragment>
);
}
The App component is a functional React component, a function that returns JSX markup. But it's also now a stateful component, thanks to the useState() Hook. This example initializes two pieces of state, name and age. This is why there are two calls to useState(), one for each state value.
You can have as many pieces of state in your component as you need. The best practice is to have one call to useState() per state value. You...