Skip to content

Commit ffc61bb

Browse files
wo-o29kimyouknow
andauthored
refactor(useRefEffect): Improve useRefEffect hook by adding generic types in docs and strict null check in implementation (toss#273)
Co-authored-by: 김윤호 yunho <kimyouknow@naver.com>
1 parent 4254542 commit ffc61bb

4 files changed

Lines changed: 21 additions & 16 deletions

File tree

.changeset/hip-shoes-show.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'react-simplikit': patch
3+
---
4+
5+
docs: Add generic type support to useRefEffect interface

packages/core/src/hooks/useRefEffect/ko/useRefEffect.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,18 @@
55
## 인터페이스
66

77
```ts
8-
function useRefEffect(
9-
callback: (element: Element) => CleanupCallback | void,
8+
function useRefEffect<RefElement extends HTMLElement>(
9+
callback: (element: RefElement) => CleanupCallback | void,
1010
deps: DependencyList
11-
): (element: Element | null) => void;
11+
): (element: RefElement | null) => void;
1212
```
1313

1414
### 파라미터
1515

1616
<Interface
1717
required
1818
name="callback"
19-
type="(element: Element) => CleanupCallback | void"
19+
type="(element: RefElement) => CleanupCallback | void"
2020
description="요소가 설정될 때 실행되는 콜백 함수예요. 이 함수는 정리 함수를 반환할 수 있어요."
2121
/>
2222

@@ -31,7 +31,7 @@ function useRefEffect(
3131

3232
<Interface
3333
name=""
34-
type="(element: Element | null) => void"
34+
type="(element: RefElement | null) => void"
3535
description="요소를 설정하는 함수예요. 이 함수를 <code>ref</code> 속성에 전달하면, 요소가 변경될 때마다 <code>callback</code>이 호출돼요."
3636
/>
3737

packages/core/src/hooks/useRefEffect/useRefEffect.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,18 @@
55
## Interface
66

77
```ts
8-
function useRefEffect(
9-
callback: (element: Element) => CleanupCallback | void,
8+
function useRefEffect<RefElement extends HTMLElement>(
9+
callback: (element: RefElement) => CleanupCallback | void,
1010
deps: DependencyList
11-
): (element: Element | null) => void;
11+
): (element: RefElement | null) => void;
1212
```
1313

1414
### Parameters
1515

1616
<Interface
1717
required
1818
name="callback"
19-
type="(element: Element) => CleanupCallback | void"
19+
type="(element: RefElement) => CleanupCallback | void"
2020
description="A callback function that is executed when the element is set. This function can return a cleanup function."
2121
/>
2222

@@ -31,7 +31,7 @@ function useRefEffect(
3131

3232
<Interface
3333
name=""
34-
type="(element: Element | null) => void"
34+
type="(element: RefElement | null) => void"
3535
description="function to set the element. Pass this function to the <code>ref</code> attribute, and the <code>callback</code> will be called whenever the element changes."
3636
/>
3737

packages/core/src/hooks/useRefEffect/useRefEffect.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ export type CleanupCallback = () => void;
99
* `useRefEffect` is a React hook that helps you set a reference to a specific DOM element and execute a callback whenever the element changes.
1010
* This hook calls a cleanup function whenever the element changes to prevent memory leaks.
1111
*
12-
* @param {(element: Element) => CleanupCallback | void} callback - A callback function that is executed when the element is set. This function can return a cleanup function.
12+
* @param {(element: RefElement) => CleanupCallback | void} callback - A callback function that is executed when the element is set. This function can return a cleanup function.
1313
* @param {DependencyList} deps - An array of dependencies that define when the callback should be re-executed. The `callback` is re-executed whenever the `deps` change.
1414
*
15-
* @returns {(element: Element | null) => void} A function to set the element. Pass this function to the `ref` attribute, and the `callback` will be called whenever the element changes.
15+
* @returns {(element: RefElement | null) => void} A function to set the element. Pass this function to the `ref` attribute, and the `callback` will be called whenever the element changes.
1616
*
1717
* @example
1818
* import { useRefEffect } from 'react-simplikit';
@@ -29,15 +29,15 @@ export type CleanupCallback = () => void;
2929
* return <div ref={ref}>Basic Example</div>;
3030
* }
3131
*/
32-
export function useRefEffect<Element extends HTMLElement = HTMLElement>(
33-
callback: (element: Element) => CleanupCallback | void,
32+
export function useRefEffect<RefElement extends HTMLElement = HTMLElement>(
33+
callback: (element: RefElement) => CleanupCallback | void,
3434
deps: DependencyList
35-
): (element: Element | null) => void {
35+
): (element: RefElement | null) => void {
3636
const preservedCallback = usePreservedCallback(callback);
3737
const cleanupCallbackRef = useRef<CleanupCallback>(() => {});
3838

3939
const effect = useCallback(
40-
(element: Element | null) => {
40+
(element: RefElement | null) => {
4141
cleanupCallbackRef.current();
4242
cleanupCallbackRef.current = () => {};
4343

0 commit comments

Comments
 (0)