I used React about 3 months.
However, I still don't
understand lifecycle hooks and how they work in React. I usually setState
in various methods and not lifecycle hooks. Thus the question - how are different React lifecycle hooks used?
I used React about 3 months.
However, I still don't
understand lifecycle hooks and how they work in React. I usually setState
in various methods and not lifecycle hooks. Thus the question - how are different React lifecycle hooks used?
Lifecycle in React usually refers to the lifecycle of a component, it's expressively portrayed on this diagram (React 16.4):
The thing the question refers to are lifecycle hooks. They are component class methods that are expected to be called by the framework itself at some moment, all of them are listed in respective reference section. Lifecycle hooks aren't expected to be called by a developer; even if this can be done, this will have code smell. The only place where lifecycle hooks should be called manually are tests.
While React.Component
non-lifecycle methods (setState
and forceUpdate
) and user methods are supposed to be called explicitly.