Skip to content

Commit

Permalink
docs: add for new hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
SukkaW committed Sep 8, 2024
1 parent 8cca7ee commit a2cd53e
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 0 deletions.
3 changes: 3 additions & 0 deletions docs/src/pages/_meta.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,18 @@
"use-error-boundary": {},
"use-intersection": {},
"use-is-client": {},
"use-is-online": {},
"use-isomorphic-layout-effect": {},
"use-local-storage": {},
"use-media-query": {},
"use-page-visibility": {},
"use-retimer": {},
"use-session-storage": {},
"use-singleton": {},
"use-stable-handler-only-when-you-know-what-you-are-doing-or-you-will-be-fired": {
"title": "useStableHandler"
},
"use-typescript-happy-callback": {},
"use-uncontrolled": {},
"-- separator components": {
"type": "separator",
Expand Down
19 changes: 19 additions & 0 deletions docs/src/pages/use-is-online.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
title: useIsOnline
---

# useIsOnline

import ExportMetaInfo from '../components/export-meta-info';

<ExportMetaInfo />

Simple React Hook for checking if you're connected to the internet. It uses `useSyncExternalStore` under the hood to support React 18 concurrent rendering and server-side rendering.

## Usage

```tsx copy
import { useIsOnline } from 'foxact/use-is-online';

useIsOnline(); // Returns a boolean value
```
19 changes: 19 additions & 0 deletions docs/src/pages/use-page-visibility.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
title: usePageVisibility
---

# usePageVisibility

import ExportMetaInfo from '../components/export-meta-info';

<ExportMetaInfo />

Simple React Hook for checking if the app is in the foreground or background. It uses `useSyncExternalStore` under the hood to support React 18 concurrent rendering and server-side rendering.

## Usage

```tsx copy
import { usePageVisibility } from 'foxact/use-page-visibility';

usePageVisibility(); // Returns a boolean value
```
49 changes: 49 additions & 0 deletions docs/src/pages/use-typescript-happy-callback.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
---
title: useTypeScriptHappyCallback
---

# useTypeScriptHappyCallback

import ExportMetaInfo from '../components/export-meta-info';

<ExportMetaInfo />

`@types/react` [uses a more general type (`Function`) to avoid breaking changes](https://github.com/DefinitelyTyped/DefinitelyTyped/issues/52873#issuecomment-845806435) for `useCallback`. `useTypeScriptHappyCallback` declared a more specific types, allows TypeScript to infer the types of callback's arguments and return value.

## Usage

```tsx copy
import { useTypeScriptHappyCallback } from 'foxact/use-typescript-happy-callback';

<input
onChange={useTypeScriptHappyCallback(
(event) => {
// ^? React.ChangeEvent<HTMLInputElement>
console.log(event.target.value); // Here the type of `event` is inferred
},
[]
)}
/>
```

Note that [`eslint-plugin-react-hooks`](https://www.npmjs.com/package/eslint-plugin-react-hooks) requires extra configuration in order to check dependency array for third-party hooks:

```json filename=".eslintrc.json" copy
{
"rules": {
"react-hooks/exhaustive-deps": [
"warn",
{
"additionalHooks": "useTypeScriptHappyCallback"
}
]
}
}
```

But if you do not want to configure it, `foxact/use-typescript-happy-callback` also provides another named export `useCallback` as an alias of `useTypeScriptHappyCallback`:

```diff
- import { useCallback } from 'react';
+ import { useCallback } from 'foxact/use-typescript-happy-callback';
```

0 comments on commit a2cd53e

Please sign in to comment.