0

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.

Ivana
  • 34
  • 7

2 Answers2

2

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.

Jesse
  • 334
  • 15
  • 3
    Please add your answer to the proposed duplicate if you feel it answers the question better/differently than the existing answers. – Heretic Monkey Sep 01 '23 at 19:49
0

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.

Amaan
  • 11
  • 3