Skip to content

Commit 71b5cfb

Browse files
committed
docs: add useEventListener
1 parent a9410e8 commit 71b5cfb

File tree

6 files changed

+84
-6
lines changed

6 files changed

+84
-6
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
<br/>
6868
- [**Lifecycles**](./docs/en/lifecycles/README.md)
6969
- [`useEffectOnce`](./docs/en/lifecycles/useEffectOnce.md) &mdash; a modified [`useEffect`](https://reactjs.org/docs/hooks-reference.html#useeffect) hook that only runs once.
70+
- [`useEventListener`](./docs/en/lifecycles/useEventListener.md) &mdash; a hook that helps you add a global event listener as early as possible.
7071
- [`useLifecycles`](./docs/en/lifecycles/useLifecycles.md) &mdash; calls `mount` and `unmount` callbacks.
7172
- [`useMountedState`](./docs/en/lifecycles/useMountedState.md) and [`useUnmountPromise`](./docs/en/lifecycles/useUnmountPromise.md) &mdash; track if component is mounted.
7273
- [`useUnmount`](./docs/en/lifecycles/useUnmount.md) &mdash; calls `unmount` callbacks.

README.zh-CN.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
<br/>
6868
- [**生命周期**](./docs/zh/lifecycles/README.md)
6969
- [`useEffectOnce`](./docs/zh/lifecycles/useEffectOnce.md) &mdash; 仅运行一次的 [`useEffect`](https://reactjs.org/docs/hooks-reference.html#useeffect)
70+
- [`useEventListener`](./docs/en/lifecycles/useEventListener.md) &mdash; 用于尽早添加全局事件监听器的 Hook。
7071
- [`useLifecycles`](./docs/zh/lifecycles/useLifecycles.md) &mdash; 调用 `mount``unmount` 回调。
7172
- [`useMountedState`](./docs/zh/lifecycles/useMountedState.md)[`useUnmountPromise`](./docs/zh/lifecycles/useUnmountPromise.md) &mdash; 跟踪组件是否已挂载。
7273
- [`useUnmount`](./docs/zh/lifecycles/useUnmount.md) &mdash; 调用 `unmount` 回调。
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# useEventListener
2+
3+
A hook that helps you add a global event listener as early as possible.
4+
5+
## Usage
6+
7+
```tsx
8+
import { useEventListener } from "@lynx-js/react-use";
9+
10+
const App = () => {
11+
// Listen to the 'exposure' event
12+
useEventListener("exposure", (e) => {
13+
console.log("exposure", e);
14+
});
15+
16+
// Listen to the 'disexposure' event
17+
useEventListener("disexposure", (e) => {
18+
console.log("disexposure", e);
19+
});
20+
21+
return (
22+
<view
23+
style={{ width: "100px", height: "100px", backgroundColor: "red" }}
24+
exposure-id="a"
25+
/>
26+
);
27+
};
28+
```
29+
30+
## Type Declarations
31+
32+
```ts
33+
declare const useEventListener: (
34+
eventName: string,
35+
listener: (...args: any[]) => void
36+
) => void;
37+
```

docs/en/mts/useVelocity.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@ const Demo = () => {
1616

1717
return (
1818
<view
19-
onTouchStart={handleTouchStartMT}
20-
onTouchMove={handleTouchMoveMT}
21-
onTouchEnd={() => {
19+
main-thread:bindtouchstart={handleTouchStartMT}
20+
main-thread:bindtouchmovee={handleTouchMoveMT}
21+
main-thread:bindtouchend={() => {
22+
"main thread";
2223
const { velocity, direction } = getVelocity();
2324
console.log(`Velocity: ${velocity}px/s, Direction: ${direction}`);
2425
}}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# useEventListener
2+
3+
用于尽早添加全局事件监听器的 Hook。
4+
5+
## 示例
6+
7+
```tsx
8+
import { useEventListener } from "@lynx-js/react-use";
9+
10+
const App = () => {
11+
// 监听 'exposure' (曝光) 事件
12+
useEventListener('exposure', (e) => {
13+
console.log("exposure", e);
14+
});
15+
16+
// 监听 'disexposure' (反曝光/消失) 事件
17+
useEventListener('disexposure', (e) => {
18+
console.log("disexposure", e);
19+
});
20+
21+
return (
22+
<view
23+
style={{ width: '100px', height: '100px', backgroundColor: 'red' }}
24+
exposure-id='a'
25+
/>
26+
);
27+
};
28+
```
29+
30+
## 类型定义
31+
32+
```ts
33+
declare const useEventListener: (
34+
eventName: string,
35+
listener: (...args: any[]) => void
36+
) => void;
37+
```

docs/zh/mts/useVelocity.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@ const Demo = () => {
1616

1717
return (
1818
<view
19-
onTouchStart={handleTouchStartMT}
20-
onTouchMove={handleTouchMoveMT}
21-
onTouchEnd={() => {
19+
main-thread:bindtouchstart={handleTouchStartMT}
20+
main-thread:bindtouchmovee={handleTouchMoveMT}
21+
main-thread:bindtouchend={() => {
22+
"main thread";
2223
const { velocity, direction } = getVelocity();
2324
console.log(`Velocity: ${velocity}px/s, Direction: ${direction}`);
2425
}}

0 commit comments

Comments
 (0)