Reimplementing the State Hook
In order to get a better understanding of how Hooks work internally in React, we are going to reimplement the useState
function from scratch. However, we are not going to implement it as an actual React Hook but as a simple JavaScript function—just to get an idea of what Hooks are actually doing.
This reimplementation is not exactly how React Hooks work internally. The actual implementation is similar, and thus, has similar constraints. However, the real implementation is more extensive than what we will be implementing here.
We are now going to start reimplementing the State Hook:
- Copy the
Chapter01_3
folder to a newChapter02_1
folder by executing the following command:$ cp -R Chapter01_3 Chapter02_1
- Open the new
Chapter02_1
folder in VS Code.
First, we need to define a function to (re)render the app, which we can use to simulate React rerendering when the Hook state changes. If we...