Advanced
What are React hooks and why are they useful?
React hooks are functions that let you use state and other React features without writing a class. They simplify component logic and enhance reusability.
Hooks such as useState
and useEffect
allow functional components to manage state and lifecycle events. This helps in writing cleaner and more maintainable code.
For instance, using useEffect
to perform side effects:
import React, { useEffect } from 'react';
const Timer = () => {
useEffect(() => {
const timer = setInterval(() => console.log('Tick'), 1000);
return () => clearInterval(timer);
}, []);
return Timer is running. Check the console!
;
};