Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 5244

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 61k
Author
Asked: November 27, 20242024-11-27T07:26:07+00:00 2024-11-27T07:26:07+00:00

React useState() – A guide for beginners

  • 61k

As a beginner, you need to know why we use useState, how its executed and why do we need it. These sections will be discussed as we deep dive into useState hook.

For a start, we need to understand why we need useState. Using vanilla Javascript code doesn't trigger component re-render. Take for example the code below:

Image description

Examining the code above, you'll probably think that the title should change because we're re-assigning it a value, right?

However, that's not what is happening. Rather, you can see that the title has a new value called “Updated” in the console but the DOM did NOT change. Why is that you ask? That is because when React renders the component a second time, the local variables don't persist, and the second reason is that, local variables don't trigger re-render/renders.

By default, the DOM only gets mounted ONCE and will never re-evaluate even if a change occurred. To update a component, 2 things need to happen, 1, for the data between render to be retained and 2, for React to render the component with the new data. Now let's try to understand that visually.

This entire process is the mounting phase of the React Component Lifecycle which only happens ONCE when the components are first evaluated and rendered for the first time

Image description

Now, we're faced with an issue where React will never repeat the same process. As soon as it's rendered for the first time, it's done. However, most of the time, we want to be able to change the values rendered on the page when a change occurs. That's when we're going to need states.

As mentioned above, React ignores any normal/vanilla variables even if a change occured. It doesn't trigger the component to re-render and even if it does, it will just re-create the variable with the same value. Even if there's a click or any other event listener, it doesn't trigger the component to run again.

In useState however, any changes to its values should result in the component function being called again.

useState returns two values, a pointer to the managed state, and the second value is a setter function that can be called later to set a new value, you can access access these values using an array destructuring.

When the setter function is called and re-renders the component, it will compare the virtual dom and the actual dom to check if there’s a change in the data or the component itself, if there is then it will replace the actual dom with the new values.

Image description

To be accurate, it's comparing the current evaluation to the previous evaluation. If there's any change, those changes is handed over to the ReactDOM which makes necessary changes in the real DOM.

This phase or state is called the Updating phase in the React component lifecycle.

Image description

Another thing to note is that the console log doesn’t print the updated value straight away, rather, it prints the value BEFORE it was updated. The reason for that is because useState doesn’t change the value right away, rather, it schedules the update.

On top of that, states are per component instance basis, meaning, if you change the state in one component, the other component state isn't affected. Think of it as a modular state, separated from the other component.

State snapshots

How do we then get the latest state snapshot? Meaning, how do we get the latest value? Well, think of it this way, imagine depositing your money in the bank, they now manage your money for you, as days go by, the interest in your account accumulates. A year later, you withdraw your money which now has interest on top.

Image description

Likewise, in React, it's like telling React to give you the latest snapshot that you asked to be managed, hence, you have an access to its previous change and manipulate it like so:

 const [detectChange, setDetectedChange] = useState(false);   setDetectedChange((previousState) => !previousState); 
Enter fullscreen mode Exit fullscreen mode

beginnersreactwebdev
  • 0 0 Answers
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 2k
  • Popular
  • Answers
  • Author

    ES6 - A beginners guide - Template Literals

    • 0 Answers
  • Author

    Understanding Higher Order Functions in JavaScript.

    • 0 Answers
  • Author

    Build a custom video chat app with Daily and Vue.js

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.