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
- Class components
- function components
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.
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.