Maintaining state using Hooks
The first React Hook API that we'll look at is called useState()
, which enables your functional React components to be stateful. Before Hooks were introduced to React, our only option for creating stateful components was to use a class so that we could access the setState()
method. In this section, you'll learn how to initialize state values and change the state of a component using Hooks.
Initial state values
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 * as React from "react"; export default function App() { const [name] = React.useState("Adam"); const [age] = React.useState(35); return ( <> <p>My name is...