-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: add docs for several hooks (#11)
- Loading branch information
Showing
19 changed files
with
685 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# useAsyncEffect | ||
|
||
Same as `useEffect`, but the effect can be asynchronous. | ||
|
||
```ts | ||
useAsyncEffect(async () => { | ||
await fetch("https://api.example.com/data"); | ||
}, []); | ||
``` | ||
|
||
## Parameters | ||
|
||
### effect | ||
|
||
- Type: `() => Promise<void>` | ||
|
||
The asynchronous effect to run. | ||
|
||
> [!NOTE] | ||
> | ||
> Destructor is not yet supported. The cleanup function returned by the effect will be ignored and discarded. | ||
### deps | ||
|
||
- Type: `DependencyList` | ||
- Default: `undefined` | ||
|
||
The dependencies of the effect, just like in `useEffect`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# useBoolean | ||
|
||
Use a boolean value. | ||
|
||
```ts | ||
const { value, set, setTrue, setFalse, toggle, reset } = useBoolean(); | ||
``` | ||
|
||
## Parameters | ||
|
||
### initialValue | ||
|
||
- Type: `boolean` | ||
- Default: `false` | ||
|
||
The initial value of the boolean. | ||
|
||
## Returns | ||
|
||
### value | ||
|
||
- Type: `boolean` | ||
|
||
The current value of the boolean. | ||
|
||
### set | ||
|
||
- Type: `Dispatch<SetStateAction<boolean>>` | ||
|
||
Set the value to any boolean. | ||
|
||
### setTrue | ||
|
||
- Type: `() => void` | ||
|
||
Set the value to `true`. | ||
|
||
### setFalse | ||
|
||
- Type: `() => void` | ||
|
||
Set the value to `false`. | ||
|
||
### toggle | ||
|
||
- Type: `() => void` | ||
|
||
Toggle the value, that is, to set the value to `true` if it is currently `false`, and to `false` if it is currently `true`. | ||
|
||
### reset | ||
|
||
- Type: `() => void` | ||
|
||
Reset the value to the initial value. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# useClickOutside | ||
|
||
Listen to click events outside of a node. | ||
|
||
```ts | ||
const modalRef = useRef<HTMLDivElement>(null); | ||
|
||
useClickOutside(modalRef, () => closeModal()); | ||
``` | ||
|
||
## Parameters | ||
|
||
### ref | ||
|
||
- Type: `RefObject<Node>` | ||
|
||
The ref of the node to listen for click events outside of. | ||
|
||
### callback | ||
|
||
- Type: `(event: MouseEvent, target: Node) => void` | ||
|
||
The function to call when a click event occurs outside of the node. | ||
|
||
The first argument is the click event, and the second argument is the target node contained inside the `ref`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# useClipboardText | ||
|
||
Use the text on the user's clipboard. | ||
|
||
```ts | ||
const { text, error, write } = useClipboardText(); | ||
``` | ||
|
||
## Parameters | ||
|
||
### options | ||
|
||
- Type: `UseClipboardTextOptions` | ||
- Default: `{}` | ||
|
||
The options to configure the hook. | ||
|
||
## Options | ||
|
||
### readOnMount | ||
|
||
- Type: `boolean` | ||
- Default: `true` | ||
|
||
Whether to immediately read the user's clipboard when the hook is mounted. | ||
|
||
If set to `false`, the returned `text` will be its default value, that is, `""`. | ||
|
||
When this option is changed from `false` to `true`, the hook will read the user's clipboard. | ||
|
||
## Returns | ||
|
||
### text | ||
|
||
- Type: `string` | ||
- Default: `""` | ||
|
||
The text on the user's clipboard. | ||
|
||
### error | ||
|
||
- Type: `Error | null` | ||
- Default: `null` | ||
|
||
The error that occurs while reading from or writing to the user's clipboard. | ||
|
||
After any successful read or write operation, `error` will be reset to `null`. | ||
|
||
### write | ||
|
||
- Type: `(text: string) => Promise<void>` | ||
|
||
Write text to the user's clipboard. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# useConstFn | ||
|
||
Memoize a function, which stays the same across rerenders. | ||
|
||
```ts | ||
const fn = useConstFn(() => { | ||
doSomething(); | ||
}); | ||
``` | ||
|
||
## Parameters | ||
|
||
### fn | ||
|
||
- Type: `Function` | ||
|
||
The function to memoize. | ||
|
||
## Returns | ||
|
||
- Type: `Function` | ||
|
||
The memoized function. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# useConst | ||
|
||
Memoize a value, which stays the same across rerenders. | ||
|
||
```ts | ||
const value = useConst(() => expensiveCompute()); | ||
``` | ||
|
||
## Parameters | ||
|
||
### factory | ||
|
||
- Type: `() => T` | ||
|
||
A function that computes the value to be memoized. | ||
|
||
## Returns | ||
|
||
- Type: `T` | ||
|
||
The memoized value. |
Oops, something went wrong.