×
☰ See All Chapters

Hooks in ReactJS

Before React 16.8 function components were state less and only Class components were able to handle state and lifecycle. Lifecycle Hooks are added to function components in version 16.8 and now the differences are so minor that you will probably never need to use a Class component in React. Hooks will not work in React class components.

Hooks allow function components to have state and to respond to lifecycle events too, and kind of make class components obsolete. They also allow function components to have a good way to handle events.

There are 3 rules for hooks:

  1. Hooks can only be called inside React function components. 

  2. You can add any number of calls to hooks you want. 

  3. Hooks can only be called at the top level of a component. Not in an if or in any other block. 

 

hooks-in-reactjs-0
 

All Chapters
Author