-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
90 additions
and
0 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,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 | ||
``` |
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,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 | ||
``` |
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,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'; | ||
``` |