Skip to content

Commit b5e42aa

Browse files
committed
feat: add docs for each custom hook
1 parent 72c7ebf commit b5e42aa

29 files changed

+1215
-4
lines changed

docs/useArray.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# `useArray`
2+
3+
A hook for managing and manipulating an array with functions like push, filter, update, remove, and clear.
4+
5+
## Arguments
6+
7+
- `defaultValue` (`array`): The initial array value.
8+
9+
## Returns
10+
11+
- An array containing:
12+
- The current array value.
13+
- Functions to manipulate the array (`push`, `filter`, `update`, `remove`, `clear`).
14+
15+
## Hooks Involved
16+
17+
- [useState](https://react.dev/reference/react/useState)
18+
19+
## How to Use
20+
21+
```js
22+
import useArray from "./useArray"
23+
24+
export default function ArrayComponent() {
25+
const { array, set, push, remove, filter, update, clear } = useArray([
26+
1, 2, 3, 4, 5, 6,
27+
])
28+
29+
return (
30+
<div>
31+
<div>{array.join(", ")}</div>
32+
<button onClick={() => push(7)}>Add 7</button>
33+
<button onClick={() => update(1, 9)}>Change Second Element To 9</button>
34+
<button onClick={() => remove(1)}>Remove Second Element</button>
35+
<button onClick={() => filter(n => n < 3)}>
36+
Keep Numbers Less Than 4
37+
</button>
38+
<button onClick={() => set([1, 2])}>Set To 1, 2</button>
39+
<button onClick={clear}>Clear</button>
40+
</div>
41+
)
42+
}
43+
```

docs/useAsync.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
### `useAsync`
1+
# `useAsync`
22

33
A hook to handle asynchronous tasks.
44

5-
## Parameters
5+
## Arguments
66

77
- `callback` (`function`): The asynchronous function to execute.
88
- `dependencies` (`array`, optional): Dependency array to control when the async function should be executed.

docs/useClickOutside.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# `useClickOutside`
2+
3+
A hook to detect clicks outside a specified element and trigger a callback.
4+
5+
## Arguments
6+
7+
- `ref` (`React.RefObject`): The ref object of the element to detect outside clicks.
8+
- `callback` (`function`): The function to call when a click outside is detected.
9+
10+
## Returns
11+
12+
- None
13+
14+
## Hooks Involved
15+
16+
- [useEventListener](./useEventListener.md)
17+
18+
## How to Use
19+
20+
```js
21+
import { useRef, useState } from "react"
22+
import useClickOutside from "./useClickOutside"
23+
24+
export default function ClickOutsideComponent() {
25+
const [open, setOpen] = useState(false)
26+
const modalRef = useRef()
27+
28+
useClickOutside(modalRef, () => {
29+
if (open) setOpen(false)
30+
})
31+
32+
return (
33+
<>
34+
<button onClick={() => setOpen(true)}>Open</button>
35+
<div
36+
ref={modalRef}
37+
style={{
38+
display: open ? "block" : "none",
39+
backgroundColor: "blue",
40+
color: "white",
41+
width: "100px",
42+
height: "100px",
43+
position: "absolute",
44+
top: "calc(50% - 50px)",
45+
left: "calc(50% - 50px)",
46+
}}
47+
>
48+
<span>Modal</span>
49+
</div>
50+
</>
51+
)
52+
}
53+
```

docs/useCookie.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# `useCookie`
2+
3+
A hook for managing cookies with get, set, and delete functionality.
4+
5+
## Arguments
6+
7+
- `name` (`string`): The name of the cookie.
8+
- `defaultValue` (`string`): The initial value of the cookie.
9+
10+
## Returns
11+
12+
- An array containing:
13+
- The current cookie value.
14+
- Functions to set and delete the cookie.
15+
16+
## Hooks Involved
17+
18+
- [useState](https://react.dev/reference/react/useState)
19+
- [useCallback](https://react.dev/reference/react/useCallback)
20+
21+
## How to Use
22+
23+
```js
24+
import useCookie from "./useCookie"
25+
26+
export default function CookieComponent() {
27+
const [value, update, remove] = useCookie("name", "John")
28+
29+
return (
30+
<>
31+
<div>{value}</div>
32+
<button onClick={() => update("Sally")}>Change Name To Sally</button>
33+
<button onClick={remove}>Delete Name</button>
34+
</>
35+
)
36+
}
37+
```

docs/useCopyToClipboard.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# `useCopyToClipboard`
2+
3+
## Arguments
4+
5+
- None
6+
7+
## Returns
8+
9+
- An array containing:
10+
- A function to copy text to the clipboard.
11+
- A boolean indicating if the copy was successful.
12+
13+
## Hooks Involved
14+
15+
- [useState](https://react.dev/reference/react/useState)
16+
17+
## How to Use
18+
19+
```js
20+
import useCopyToClipboard from "./useCopyToClipboard"
21+
22+
export default function CopyToClipboardComponent() {
23+
const [copyToClipboard, { success }] = useCopyToClipboard()
24+
25+
return (
26+
<>
27+
<button onClick={() => copyToClipboard("This was copied")}>
28+
{success ? "Copied" : "Copy Text"}
29+
</button>
30+
<input type="text" />
31+
</>
32+
)
33+
}
34+
```

docs/useDarkMode.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# `useDarkMode`
2+
3+
A hook to manage dark mode state and apply dark mode styling based on user preference and localStorage.
4+
5+
## Arguments
6+
7+
- None
8+
9+
## Returns
10+
11+
- An array containing:
12+
- A boolean indicating if dark mode is enabled.
13+
- A function to toggle dark mode.
14+
15+
## Hooks Involved
16+
17+
- [useLocalStorage](./useLocalStorage.md)
18+
- [useMediaQuery](./useMediaQuery.md)
19+
- [useEffect](https://react.dev/reference/react/useEffect)
20+
21+
## How to Use
22+
23+
```js
24+
/* body.css file:
25+
body.dark-mode {
26+
background-color: #333;
27+
}
28+
*/
29+
30+
import useDarkMode from "./useDarkMode"
31+
import "./body.css"
32+
33+
export default function DarkModeComponent() {
34+
const [darkMode, setDarkMode] = useDarkMode()
35+
36+
return (
37+
<button
38+
onClick={() => setDarkMode(prevDarkMode => !prevDarkMode)}
39+
style={{
40+
border: `1px solid ${darkMode ? "white" : "black"}`,
41+
background: "none",
42+
color: darkMode ? "white" : "black",
43+
}}
44+
>
45+
Toggle Dark Mode
46+
</button>
47+
)
48+
}
49+
```

docs/useDebounce.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# `useDebounce`
2+
3+
A hook to debounce a callback function with a specified delay.
4+
5+
## Arguments
6+
7+
- `callback` (`function`): The function to debounce.
8+
- `delay` (`number`): The debounce delay in milliseconds.
9+
- `dependencies` (`array`): The dependencies for the effect.
10+
11+
## Returns
12+
13+
- None
14+
15+
## Hooks Involved
16+
17+
- [useTimeout](./useTimeout.md)
18+
- [useEffect](https://react.dev/reference/react/useEffect)
19+
20+
## How to Use
21+
22+
```js
23+
import { useState } from "react"
24+
import useDebounce from "./useDebounce"
25+
26+
export default function DebounceComponent() {
27+
const [count, setCount] = useState(10)
28+
useDebounce(() => alert(count), 1000, [count])
29+
30+
return (
31+
<div>
32+
<div>{count}</div>
33+
<button onClick={() => setCount(c => c + 1)}>Increment</button>
34+
</div>
35+
)
36+
}
37+
```

docs/useDebugInformation.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# `useDebugInformation`
2+
3+
A hook to provide debugging information, including render count and changed props.
4+
5+
## Arguments
6+
7+
- `componentName` (`string`): The name of the component for debugging.
8+
- `props` (`object`): The current props of the component.
9+
10+
## Returns
11+
12+
- An object containing:
13+
- `count`: The number of times the component has rendered.
14+
- `changedProps`: An object detailing the props that have changed.
15+
- `timeSinceLastRender`: The time in milliseconds since the last render.
16+
- `lastRenderTimestamp`: The timestamp of the last render.
17+
18+
## Hooks Involved
19+
20+
- [useRenderCount](./useRenderCount.md)
21+
- [useRef](https://react.dev/reference/react/useRef)
22+
- [useEffect](https://react.dev/reference/react/useEffect)
23+
24+
## How to Use
25+
26+
```js
27+
import useDebugInformation from "./useDebugInformation"
28+
import useToggle from "../useToggle/useToggle"
29+
import { useState } from "react"
30+
31+
export default function DebugInformationComponent() {
32+
const [boolean, toggle] = useToggle(false)
33+
const [count, setCount] = useState(0)
34+
35+
return (
36+
<>
37+
<ChildComponent boolean={boolean} count={count} />
38+
<button onClick={toggle}>Toggle</button>
39+
<button onClick={() => setCount(prevCount => prevCount + 1)}>
40+
Increment
41+
</button>
42+
</>
43+
)
44+
}
45+
46+
function ChildComponent(props) {
47+
const info = useDebugInformation("ChildComponent", props)
48+
49+
return (
50+
<>
51+
<div>{props.boolean.toString()}</div>
52+
<div>{props.count}</div>
53+
<div>{JSON.stringify(info, null, 2)}</div>
54+
</>
55+
)
56+
}
57+
```

docs/useDeepCompareEffect.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# `useDeepCompareEffect`
2+
3+
A hook for running an effect with dependencies that are deeply compared.
4+
5+
## Arguments
6+
7+
- `callback` (`function`): The effect function to run.
8+
- `dependencies` (`array`): The dependencies for the effect, deeply compared.
9+
10+
## Returns
11+
12+
- None
13+
14+
## Hooks Involved
15+
16+
- [useEffect](https://react.dev/reference/react/useEffect)
17+
- [useRef](https://react.dev/reference/react/useRef)
18+
19+
## libraries Involved
20+
- isEqual `lodash --> lodash/fp/isEqual`
21+
22+
## How to Use
23+
24+
```js
25+
import { useEffect, useState, useRef } from "react"
26+
import useDeepCompareEffect from "./useDeepCompareEffect"
27+
28+
export default function DeepCompareEffectComponent() {
29+
const [age, setAge] = useState(0)
30+
const [otherCount, setOtherCount] = useState(0)
31+
const useEffectCountRef = useRef()
32+
const useDeepCompareEffectCountRef = useRef()
33+
34+
const person = { age: age, name: "Sergey" }
35+
36+
useEffect(() => {
37+
useEffectCountRef.current.textContent =
38+
parseInt(useEffectCountRef.current.textContent) + 1
39+
}, [person])
40+
41+
useDeepCompareEffect(() => {
42+
useDeepCompareEffectCountRef.current.textContent =
43+
parseInt(useDeepCompareEffectCountRef.current.textContent) + 1
44+
}, [person])
45+
46+
return (
47+
<div>
48+
<div>
49+
useEffect: <span ref={useEffectCountRef}>0</span>
50+
</div>
51+
<div>
52+
useDeepCompareEffect: <span ref={useDeepCompareEffectCountRef}>0</span>
53+
</div>
54+
<div>Other Count: {otherCount}</div>
55+
<div>{JSON.stringify(person)}</div>
56+
<button onClick={() => setAge(currentAge => currentAge + 1)}>
57+
Increment Age
58+
</button>
59+
<button onClick={() => setOtherCount(count => count + 1)}>
60+
Increment Other Count
61+
</button>
62+
</div>
63+
)
64+
}
65+
```

0 commit comments

Comments
 (0)