16
Blog @ Codonomics: 3 Ways To Initialize State In React
source link: https://blog.codonomics.com/2020/04/3-ways-to-initialize-state-in-react.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
3 Ways To Initialize State In React
The 3 ways are:
- Class Component that uses constructor to initialise state.
- Class Component that uses static/class property to initialise state.
- Functional Component (aka Function) that uses React Hook viz. useState to initialise state.
But a developer is smart. With the advent of ESLint this issue was resolved. That said, Babel users start to leverage static/class property feature available in Babel, for it is concise and looks cleaner. Note this class property isn't a feature of Javascript as yet and so would result in constructor being generated for you by Babel.
In the mean time, React introduced easier way to define stateless components with just functions. These are called Stateless / Functional Components. And with time React also introduced React Hooks. One of the hooks were useState that hooks state into a component. With the advent of this, the community started to leverage this one over the above methods.
Your mileage and preference may vary. This project is aimed to help you decide the flavor you prefer and start adopting.
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK