What is the State Initialization Pattern?

Here's a quick introduction to patterns in React with state initialization!

We'll cover the following

Setting the Value of the State

To initialize means to set the value of something. Well, technically, initialization is setting the value of something right when it is created/defined and any subsequent value setting is referred to as ‘assignment’. However, we’re going with the first definition to make things simpler. The state initializer pattern exists to make it easy for the consumer of your custom hook to set the “value of state”.

Note that the state initializer pattern doesn’t give full control over setting the “value of the state” every single time. It mostly allows for setting initial state resetting it.

This is not the same as having full control over setting the state value, but it offers similar benefits as you’ll see soon.

In our implementation of the custom hook, we’ve done the following:

Get hands-on with 1200+ tech skills courses.