-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCustomHooks.jsx
More file actions
54 lines (46 loc) · 1.64 KB
/
CustomHooks.jsx
File metadata and controls
54 lines (46 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// CustomHooks.jsx
import React, { useState } from 'react';
/*
✅ What is a Custom Hook?
A custom hook is a reusable JavaScript function that starts with 'use' and uses other hooks inside.
It helps you extract and share logic between components without repeating code.
*/
// 🔁 Custom Hook: useToggle
function useToggle(initialValue = false) {
const [value, setValue] = useState(initialValue);
const toggle = () => setValue(prev => !prev);
return [value, toggle];
}
// 🔁 Another Custom Hook: useCounter
function useCounter(initial = 0, step = 1) {
const [count, setCount] = useState(initial);
const increment = () => setCount(c => c + step);
const decrement = () => setCount(c => c - step);
const reset = () => setCount(initial);
return { count, increment, decrement, reset };
}
// ✅ Example Component using useToggle and useCounter
const CustomHooks = () => {
const [isVisible, toggleVisibility] = useToggle();
const counter = useCounter(0, 5);
return (
<div style={{ padding: '20px' }}>
<h1>Custom Hooks Example</h1>
<section>
<h2>useToggle Hook</h2>
<button onClick={toggleVisibility}>
{isVisible ? 'Hide' : 'Show'} Message
</button>
{isVisible && <p>Hello from useToggle!</p>}
</section>
<section>
<h2>useCounter Hook</h2>
<p>Count: {counter.count}</p>
<button onClick={counter.increment}>Increment</button>
<button onClick={counter.decrement}>Decrement</button>
<button onClick={counter.reset}>Reset</button>
</section>
</div>
);
};
export default CustomHooks;