Why Need React Hooks (React 16.8)

oshada viraj
Jul 22 · 2 min read

Hooks are new addition coming with react 16.8 it will provide state management for react function components. basically, react have two type of components

  1. Class components
  2. function components

Class components

react class based component is an extended version of javascript class. it extends React.Component and render() method is required. example for react class based component.

class based component can easily manage state just define state object & assign value to sate using setState() function and react class component provide react life cycle methods like componeneWillMount() for manage some rendering condition inside of component.

Functional components

functional component is a regular pure javaScript function. accepts props as an argument. and return some JSX. sample for react functional components.


Why need React Hooks ?

functional components can’t managing state , lifecycle events. but from react 16.8 using react Hooks can managing sate , lifecycle events inside functional component very easy. example bellow

this is how react hooks manage the sate inside of functional component. useState is the hooks manage the state

const [name, setName] = useState('');

name is the property inside of state , setName() is the method change the value of name property.

useEffect is the hooks manage the life cycle methods example componentDidMount() componentWillUpdate() when component mount , update useEffect() hooks will call and update state values by calling setName() , setAge()

This is the basic understanding for react Hooks and why we need Hooks.

Available Hooks Types

ref : https://reactjs.org/docs/hooks-reference.html

oshada viraj

Written by

Software Engineer

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade