Mid From PDF React React.js

How do you create custom hooks?

Custom hooks are just functions that use hooks.

✅ Example:

import { useState, useEffect } from 'react';

function useWindowWidth() {

const [width, setWidth] = useState(window.innerWidth);

useEffect(() => {

const handleResize = () => setWidth(window.innerWidth);

window.addEventListener('resize', handleResize);

return () => window.removeEventListener('resize', handleResize);

}, []);

return width;
}

// Usage

function Component() {

const width = useWindowWidth();
return <p>Window width: {width}</p>;
}

More from React.js Tutorial

All questions for this course
Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details