Have you been asked questions like this during interviews?:
(This is a classic question of Chinese style interview, we call it ‘八股文’, which basically means that the questions is too classic and everyone can answer it by remembering it)
In this article, we will learn the software engineering principles behind the scenes. After learning these principles, you will easily understand how other state management tools(redux, jotal) work.
loops
, conditions
, or nested
functionsA state is nothing more than a
getter
and asetter
.
You can see the concept of state
exists in almost all programming languages. (Some programming languages use stream
instead given that the issues state causes)
Let’s take an example using Javascript:
Say, we wanna build a bank account system. Whether we can withdraw a certain amount of money from a bank account depends upon the history of deposit and withdrawal transactions.
1 | function bankAccount(deposit){ |
As you can see, in javascript, the state is pretty simple:
init
: use keyword let to define a state let balance = 50;
setter
: use = to assign(set) a new value to state balance = 666;
getter
: use the variable name to get the value balance
Once you understand the essence of state, you can easily understand react state too.
React state is nothing but a state abstraction with a rerender mechanism in react component.
I will walk through how to build a useState
step by step. Before that, let’s sort out what features we need to react state.
init
: init a state, in react useState()
init a state in a component.getter
: the first value of return array of useState()
is the getter
.setter
: the second value of return array of useState()
is the setter
.rerender
: when set a new state, it should trigger react component rerender
using the latest state.1 | // init a state balance with value 666 |
Here is how we use our state:
1 | const App = () => { |
Now it works pretty well! But as our react component becomes more complex, we find that this implementation is not sophisticated enough, it will break when there are multiple states are declared.
It breaks when we add a name state:
1 | const App = () => { |
This is because we only store states
in a single variable. Now we change it to Array(List)
, we need a cursor
to indicate the index of the current state, so that we can get the correct state:
1 | let state = []; |
Now let’s take a look at what happens when we call the code below:
1 | const [balance, setBalance] = useState(666); |
As you can see, the order is very important because we use array and index to help useState
to map to correct state’s getter and setter.
Let’s take a look if we write hook
inside a conditional statement:
1 | let firstRender = true; |
As you can see, the name
state is mapped to the wrong state using the wrong cursor
. This is why we cannot break the order of state execution using conditional or loop statements in react.
Frontend, React, Software Engineering — Apr 10, 2024
Made with ❤ and at Earth.