Crafting a State in a Function Component
Learn about the essential keypoints for implementing a state management system within a function component.
User actions and event handling
When we visit a typical web page, it asks for our username and password. After we log in, the website displays its content, such as blogs, tweets, or videos, in chronological order. We can vote on them and post comments—a very typical web experience these days.
When we surf a website like that as users, we don't put too much thought into how any of the actions are implemented, nor do we care about the order in which each is fired. However, when it comes to building the site ourselves, each action and the time at which each gets fired start to become important.
An
|--x---x---x-x--x--x------> user event|--a---a---a-a--a--a------> action handler
In the figure above, basically, an x
in the user event
series is followed by an a
in the user event
series. Based on this, we can start to handle a user action. Let's turn ourselves to a "Hello World" Title
component with a button inside. Each time we click the button, a counter is incremented by one and appended after "Hello World+", as shown in the figure below.
To implement that, we start with a ...