Custom Hooks
Custom Hooks
Custom Hooks 3 of 5
State
Functional
function MyComponent() {
const [count, setCount] = useState(0);
return (
<div>
https://projects.100xdevs.com/pdf/3Vhp7rCJUVjnvFuPxZSZ/Custom-Hooks-3 1/19
10/11/2024, 01:26 Projects | 100xDevs
<p>{count}</p>
<button onClick={incrementCount}>Increment</button>
Custom Hooks 3 of 5
</div>
);
}
Class Based
incrementCount = () => {
this.setState({ count: this.state.count + 1 });
}
render() {
return (
<div>
<p>{this.state.count}</p>
<button onClick={this.incrementCount}>Increment</button
</div>
);
}
}
Lifecycle events
Functional
https://projects.100xdevs.com/pdf/3Vhp7rCJUVjnvFuPxZSZ/Custom-Hooks-3 2/19
10/11/2024, 01:26 Projects | 100xDevs
return () => {
// Cleanup code (similar to componentWillUnmount)
};
}, []);
// Render UI
}
Class based
componentWillUnmount() {
// Clean up (e.g., remove event listeners or cancel subscriptio
}
render() {
// Render UI
}
}
Functional solution
function App() {
const [render, setRender] = useState(true);
useEffect(() => {
setInterval(() => {
setRender(r => !r);
}, 5000)
https://projects.100xdevs.com/pdf/3Vhp7rCJUVjnvFuPxZSZ/Custom-Hooks-3 3/19
10/11/2024, 01:26 Projects | 100xDevs
}, []);
Custom Hooks 3 of 5
return (
<>
{render ? <MyComponent /> : <div></div>}
</>
)
}
function MyComponent() {
useEffect(() => {
console.error("component mounted");
return () => {
console.log("component unmounted");
};
}, []);
return <div>
From inside my component
</div>
}
1. useState
2. useEffect
3. useMemo
4. useCallback
https://projects.100xdevs.com/pdf/3Vhp7rCJUVjnvFuPxZSZ/Custom-Hooks-3 4/19
10/11/2024, 01:26 Projects | 100xDevs
hooks
Hooks that you create yourself, so other people can use them
are called custom hooks.
https://projects.100xdevs.com/pdf/3Vhp7rCJUVjnvFuPxZSZ/Custom-Hooks-3 5/19
10/11/2024, 01:26 Projects | 100xDevs
Custom Hooks 3 of 5
3 - Data fetching hooks
Data fetching hooks can be used to encapsulate all the logic to
fetch the data from your backend
function App() {
const [todos, setTodos] = useState([])
useEffect(() => {
axios.get("https://sum-server.100xdevs.com/todos")
.then(res => {
setTodos(res.data.todos);
})
}, [])
return (
<>
{todos.map(todo => <Track todo={todo} />)}
</>
)
}
https://projects.100xdevs.com/pdf/3Vhp7rCJUVjnvFuPxZSZ/Custom-Hooks-3 6/19
10/11/2024, 01:26 Projects | 100xDevs
Custom Hooks 3 of 5
function useTodos() {
const [todos, setTodos] = useState([])
useEffect(() => {
axios.get("https://sum-server.100xdevs.com/todos")
.then(res => {
setTodos(res.data.todos);
})
}, [])
return todos;
}
function App() {
const todos = useTodos();
return (
<>
https://projects.100xdevs.com/pdf/3Vhp7rCJUVjnvFuPxZSZ/Custom-Hooks-3 7/19
10/11/2024, 01:26 Projects | 100xDevs
function useTodos() {
const [loading, setLoading] = useState(true);
const [todos, setTodos] = useState([])
useEffect(() => {
axios.get("https://sum-server.100xdevs.com/todos")
.then(res => {
setTodos(res.data.todos);
setLoading(false);
})
}, [])
return {
todos: todos,
loading: loading
};
https://projects.100xdevs.com/pdf/3Vhp7rCJUVjnvFuPxZSZ/Custom-Hooks-3 8/19
10/11/2024, 01:26 Projects | 100xDevs
Custom Hooks 3 of 5
function App() {
const { todos, loading } = useTodos();
if (loading) {
return <div>
Loading...
</div>
}
return (
<>
{todos.map(todo => <Track todo={todo} />)}
</>
)
}
function useTodos(n) {
const [loading, setLoading] = useState(true);
const [todos, setTodos] = useState([])
function getData() {
https://projects.100xdevs.com/pdf/3Vhp7rCJUVjnvFuPxZSZ/Custom-Hooks-3 9/19
10/11/2024, 01:26 Projects | 100xDevs
axios.get("https://sum-server.100xdevs.com/todos")
.then(res => {
Custom Hooks 3 of 5
setTodos(res.data.todos);
setLoading(false);
})
}
useEffect(() => {
setInterval(() => {
getData();
}, n * 1000)
getData();
}, [n])
return {
todos: todos,
loading: loading
};
}
function App() {
const { todos, loading } = useTodos(5);
if (loading) {
return <div>
Loading...
</div>
}
return (
<>
{todos.map(todo => <Track todo={todo} />)}
</>
)
}
https://projects.100xdevs.com/pdf/3Vhp7rCJUVjnvFuPxZSZ/Custom-Hooks-3 10/19
10/11/2024, 01:26 Projects | 100xDevs
Final solution
function useTodos(n) {
const [todos, setTodos] = useState([])
const [loading, setLoading] = useState(true);
useEffect(() => {
const value = setInterval(() => {
axios.get("https://sum-server.100xdevs.com/todos")
.then(res => {
setTodos(res.data.todos);
setLoading(false);
})
}, n * 1000)
axios.get("https://sum-server.100xdevs.com/todos")
.then(res => {
setTodos(res.data.todos);
setLoading(false);
})
return () => {
clearInterval(value)
}
}, [n])
function App() {
const {todos, loading} = useTodos(10);
if (loading) {
return <div> loading... </div>
}
https://projects.100xdevs.com/pdf/3Vhp7rCJUVjnvFuPxZSZ/Custom-Hooks-3 11/19
10/11/2024, 01:26 Projects | 100xDevs
return (
<>
Custom Hooks 3 of 5
{todos.map(todo => <Track todo={todo} />)}
</>
)
}
For example -
function Profile() {
const { data, error, isLoading } = useSWR('https://sum-server.100x
https://projects.100xdevs.com/pdf/3Vhp7rCJUVjnvFuPxZSZ/Custom-Hooks-3 12/19
10/11/2024, 01:26 Projects | 100xDevs
https://swr.vercel.app/
4 - Browser functionality
related hooks
1. useIsOnline hook
Create a hook that returns true or false based on weather the
user is currently online
Solution
function useIsOnline() {
const [isOnline, setIsOnline] = useState(window.navigator.onLi
useEffect(() => {
https://projects.100xdevs.com/pdf/3Vhp7rCJUVjnvFuPxZSZ/Custom-Hooks-3 13/19
10/11/2024, 01:26 Projects | 100xDevs
return isOnline;
}
function App() {
const isOnline = useIsOnline(5);
return (
<>
{isOnline ? "You are online yay!" : "You are not online"}
</>
)
}
2. useMousePointer hook
Create a hook that returns you the current mouse pointer
position.
https://projects.100xdevs.com/pdf/3Vhp7rCJUVjnvFuPxZSZ/Custom-Hooks-3 14/19
10/11/2024, 01:26 Projects | 100xDevs
Custom Hooks 3 of 5
window.addEventListener('mousemove', handleMouseMove);
Solution
useEffect(() => {
window.addEventListener('mousemove', handleMouseMove);
return () => {
window.removeEventListener('mousemove', handleMouseMo
};
}, []);
return position;
};
function App() {
const mousePointer = useMousePointer();
return (
<>
Your mouse position is {mousePointer.x} {mousePointer.y}
</>
https://projects.100xdevs.com/pdf/3Vhp7rCJUVjnvFuPxZSZ/Custom-Hooks-3 15/19
10/11/2024, 01:26 Projects | 100xDevs
)
}
Custom Hooks 3 of 5
5 - Performance/Timer
based
1. useInterval
Create a hook that runs a certain callback function every n
seconds.
You have to implement useInterval which is being used in the
code below -
function App() {
const [count, setCount] = useState(0);
useInterval(() => {
setCount(c => c + 1);
}, 1000)
return (
<>
Timer is at {count}
https://projects.100xdevs.com/pdf/3Vhp7rCJUVjnvFuPxZSZ/Custom-Hooks-3 16/19
10/11/2024, 01:26 Projects | 100xDevs
</>
)
Custom Hooks 3 of 5
}
Solution
2. useDebounce
Create a hook that debounces a value given
https://projects.100xdevs.com/pdf/3Vhp7rCJUVjnvFuPxZSZ/Custom-Hooks-3 17/19
10/11/2024, 01:26 Projects | 100xDevs
return (
<input
type="text"
value={inputValue}
onChange={(e) => setInputValue(e.target.value)}
placeholder="Search..."
/>
);
};
Solution
useEffect(() => {
// Set up a timer to update the debounced value after the sp
const timerId = setTimeout(() => {
setDebouncedValue(value);
}, delay);
return debouncedValue;
};
https://projects.100xdevs.com/pdf/3Vhp7rCJUVjnvFuPxZSZ/Custom-Hooks-3 18/19
10/11/2024, 01:26 Projects | 100xDevs
Custom Hooks 3 of 5
https://projects.100xdevs.com/pdf/3Vhp7rCJUVjnvFuPxZSZ/Custom-Hooks-3 19/19