skip to Main Content

I’ve learn what useState is and how to use it, however I don’t know what the difference it has to just a regular variable.

I’ve tried setting a normal variable in React and it still works the same, but there must be a difference.

2

Answers


  1. The difference between a normal variable and useState is that, useState gives you two values the variable and the setter function setState() when this setter function is called your state changes but it also causes your component to re-render and show the new state. If you just use a variable then it won’t re-render you’ll have to change it manually. I assume the reason you weren’t encountering issues when using just a normal variable was because you weren’t changing it.

    Login or Signup to reply.
  2. To explain in simple terms- useState and variables both can store a value, however the value inside a variable will reset whenever the component is updated(re-rendered). However state holds the updated value whenever the rerender is triggered.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search