Skip to content

Commit

Permalink
feat: review后修改
Browse files Browse the repository at this point in the history
  • Loading branch information
askwuxue committed Jul 16, 2024
1 parent c953389 commit a2ebe93
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 4 deletions.
29 changes: 29 additions & 0 deletions packages/hooks/src/useEventListener/demo/demo3.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* title: Listen to multiple events.
* desc: Mouse hover or over the button to preview.
*
* title.zh-CN: 监听多个事件
* desc.zh-CN: 鼠标移入移出按钮查看效果。
*/

import React, { useRef, useState } from 'react';
import { useEventListener } from 'ahooks';

export default () => {
const ref = useRef(null);
const [value, setValue] = useState('');

useEventListener(
['mouseenter', 'mouseleave'],
(ev) => {
setValue(ev.type);
},
{ target: ref },
);

return (
<button ref={ref} type="button">
You Option is {value}
</button>
);
};
4 changes: 4 additions & 0 deletions packages/hooks/src/useEventListener/index.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ Use addEventListener elegant by Hook.

<code src="./demo/demo2.tsx" />

### Listen for multiple events

<code src="./demo/demo3.tsx" />

## API

```typescript
Expand Down
8 changes: 4 additions & 4 deletions packages/hooks/src/useEventListener/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,17 @@ function useEventListener(eventName: string | string[], handler: noop, options:

const eventNameArray = Array.isArray(eventName) ? eventName : [eventName];

eventNameArray.forEach((eventName) => {
targetElement.addEventListener(eventName, eventListener, {
eventNameArray.forEach((event) => {
targetElement.addEventListener(event, eventListener, {
capture: options.capture,
once: options.once,
passive: options.passive,
});
});

return () => {
eventNameArray.forEach((eventName) => {
targetElement.removeEventListener(eventName, eventListener, {
eventNameArray.forEach((event) => {
targetElement.removeEventListener(event, eventListener, {
capture: options.capture,
});
});
Expand Down
4 changes: 4 additions & 0 deletions packages/hooks/src/useEventListener/index.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ nav:

<code src="./demo/demo2.tsx" />

### 监听多个事件

<code src="./demo/demo3.tsx" />

## API

```typescript
Expand Down

0 comments on commit a2ebe93

Please sign in to comment.