Forgetting to Remember People?

Today I want to talk especially about forgetting and remembering people. People. We need People. It’s important that we remember people. But do we find it easy to forget them? When we are…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




useState Hook In React

A simplified version of probably the most important hook in react

Before reading this article it is very important that you should have a basic knowledge of React and how to run a react app on your laptop. If you do not know that, I will recommend reading my article on getting started with React so that you have the basic setup ready.

Learn to use useState in your functional components.

Few facts about React Hooks:

Now the above code will give an error as “React Hook useState is called conditionally”. React hooks must be called In the exact same order for every component render.

useState Hook:

import the useState hook from React like this:

Just call it in a function and pass the default state. useState always returns an array with 2 values. The first thing in the array is always the current state and the second is the function that will allow you to update the current state to the next state.

Now write a function setCount inside decrementCount as:

Here, the prevCount is storing the last count and we are not using count-1 because count stores the default state and if we use setCount twice in a row they will only over-write each other and not make any difference.

Similarly, create a function for incrementing:

2 ways of calling useState Hook:

The first way is to directly pass the default value at the start and the second way is to use the functional way of doing it so that our function does not run every time we re-render our component. If the initial state has very slow computation and we do not want our website to slow down we should use the function version so that it runs only one time.

What to do if you want to use objects as default states?

You all must be wondering if we can just update the part of the object we want to change and leave what should not be touched while using hooks. The answer is NO!

In functional components, while using hooks we have to spread the initial stage and then update the objects we want to change. If you are using objects inside of a state please remember to update the old values as well because they do not get updated automatically. We can do it as follows:

It is a common practice to use multiple state hooks for handling different things so that each part can be handled individually without any problems.

We can use multiple hooks for count and theme as follows:

useState can be scary to look at for the first time but once you understand how to use it then literally it’s the best thing you will ever learn.

Add a comment

Related posts:

Cross Country Road Trip In Hearse Is To Die For

The great American road trip is one of the most desirable and passionate experiences for car enthusiasts young and old alike. All across the nation, there are gearheads willing to push their cars to…

The Use of College For Computer Science

The way to a brighter future, the biggest guarantee of higher life income and all that. But is it really? Specifically for Computer science/programming/the infinite things one could do with thoose…

8 Things That Happen When You Lose Weight

Weight Lose Danny Lampton (Light & The Weight) weight loss tips Paper Weight Entertainment weight lose Lose Weight Fast Habit Fitness Club In the majority of cases, losing weight will drastically…