The AA BB CC pattern: This pattern is one of the most common in rap music. It involves two lines of verse that rhyme with each other (AA), followed by two more lines that rhyme with each other (BB)…
Okay, but what are hooks really?
Great! How do I do that?
First, Update your React and React DOM dependency in your project. Hooks are freshly released on React 16.8.0 today!
Now let’s take a look on the useState
hook. Here is the demo:
We need to have a simple class component with state for comparison. The easiest example I can imagine is of an input form:
Now let’s rewrite it using useState
hook. We'll import it from react
package so we don't have to write React.useState
all the time.
Now change our NameForm
into a function component
useState
hook takes one argument, which is the initial state, and it returns two values: the current state and a function that can be used to update the state. You can replace the state initialization in the constructor:
into this:
So this means we have a state named value
and we can update it by calling on setValue
function. Let’s use it on our render method:
Onchange
props no longer call a handleChange
method, instead we have an arrow function that will call setValue
function, which update our state
. Oh, and unlike this.setState
in the class component, updating a state variable using hooks always replace it instead of merging it.
Then call on useState
as many times as you need.
And that’s it with useState
hook, really!
Oh, it’s pretty easy!
Got you covered here:
The important thing to remember is that hooks don’t work in a class component. They are made for function components.
The useState
hook enables function components to access React’s internal state and update it. The state can be any data type: string, number, boolean, array, or object. useState
accept one argument: the initial data, and it returns an array of two values: the current state value and the function/ method that can be used to update the state.
There are more than just useState
hook, but let’s cover each hook in their own single post.
Until next time!
Bangladeshi Software companies probably prefer service providing approach over own product development because service brings money quickly. Sometimes we see that service oriented companies also…
Argumentasiku berdistraksi Menelisik diri dalam naungan sepi Ayunkan segala persepsi tiada henti Hanya anggapan belaka yang menggila. “Elegi” is published by Egastian Tiyomadi Putra.
The ideological separation of the heart and mind represents our separation of our thoughts from our emotions. See the perspective where these are the same because to fully know anything is to feel…