• python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Facebook Twitter Instagram
Devs Fixed
  • python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Devs Fixed
Home ยป Resolved: React JS Constructor vs useState what’s the difference?

Resolved: React JS Constructor vs useState what’s the difference?

0
By Isaac Tonny on 16/06/2022 Issue
Share
Facebook Twitter LinkedIn

Question:

What is the difference between constructor and useState, I tried them both and they work on same solution.
UseState
vs
constructor

Answer:

Generally, these are two different approaches in React. The constructor type is part of the OOP React approach with the so called class components. There you have class methods instead of functions. And the state is accessed through this.state.<property>.
The other approach is the function components. That’s the more modern way of doing stuff with React. useState() is a React hook, responsible for a particular state.
They work the same, however the function-based approach is getting more and more common, so I would suggest to switch to function components, unless something requires the explicit usage of class components.

If you have better answer, please add a comment about this, thank you!

reactjs
Share. Facebook Twitter LinkedIn

Related Posts

Resolved: correct policy to get secrets on local go app

22/03/2023

Resolved: Vue 3 watchEffect dependency tracking after asynchronous code

22/03/2023

Resolved: Watching state object in pinia doesn’t fire when object changes

22/03/2023

Leave A Reply

© 2023 DEVSFIX.COM

Type above and press Enter to search. Press Esc to cancel.