Skip to content

Commit

Permalink
Merge branch 'master' into fix/useAntdTable_default_params
Browse files Browse the repository at this point in the history
  • Loading branch information
hchlq authored Aug 22, 2023
2 parents 369693a + 2ff3ed9 commit b251b7c
Show file tree
Hide file tree
Showing 12 changed files with 64 additions and 34 deletions.
2 changes: 1 addition & 1 deletion packages/hooks/src/useCreation/index.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ nav:

`useCreation``useMemo``useRef` 的替代品。

因为 `useMemo` 不能保证被 memo 的值一定不会被重计算,而 `useCreation` 可以保证这一点。以下为 React 官方文档中的介绍:
因为 `useMemo` 不能保证被 memo 的值一定不会被重新计算,而 `useCreation` 可以保证这一点。以下为 React 官方文档中的介绍:

> **You may rely on useMemo as a performance optimization, not as a semantic guarantee.** In the future, React may choose to “forget” some previously memoized values and recalculate them on next render, e.g. to free memory for offscreen components. Write your code so that it still works without `useMemo` — and then add it to optimize performance.
Expand Down
4 changes: 2 additions & 2 deletions packages/hooks/src/useDocumentVisibility/index.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ const documentVisibility = useDocumentVisibility();

### Result

| 参数 | 说明 | 类型 |
| ------------------ | ------------------------------------ | -------------------------------------------------- |
| 参数 | 说明 | 类型 |
| ------------------ | ------------------------------ | -------------------------------------------------- |
| documentVisibility | 判断 document 是否处于可见状态 | `visible`\| `hidden` \| `prerender` \| `undefined` |
8 changes: 4 additions & 4 deletions packages/hooks/src/useDrop/index.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ useDrag<T>(

#### DragImageOptions

| 参数 | 说明 | 类型 | 默认值 |
| ------- | ----------------------------------------------------------- | ------------------- | ------ |
| 参数 | 说明 | 类型 | 默认值 |
| ------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------- | ------ |
| image | An image Element element to use for the drag feedback image. The image will typically be an [`<img>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/img) element but it can also be a [`<canvas>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/canvas) or any other visible element | `string \| Element` | - |
| offsetX | the horizontal offset within the image | `number` | 0 |
| offsetY | the vertical offset within the image | `number` | 0 |
| offsetX | the horizontal offset within the image | `number` | 0 |
| offsetY | the vertical offset within the image | `number` | 0 |

### useDrop

Expand Down
8 changes: 4 additions & 4 deletions packages/hooks/src/useDrop/index.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ useDrag<T>(

#### DragImageOptions

| 参数 | 说明 | 类型 | 默认值 |
| ------- | ---------------------------- | ------------------- | ------ |
| 参数 | 说明 | 类型 | 默认值 |
| ------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ------------------- | ------ |
| image | 拖拽过程中跟随鼠标指针的图像。图像通常是一个 [`<img>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/img) 元素,但也可以是 [`<canvas>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/canvas) 或任何其他图像元素。 | `string \| Element` | - |
| offsetX | 水平偏移 | `number` | 0 |
| offsetY | 垂直偏移 | `number` | 0 |
| offsetX | 水平偏移 | `number` | 0 |
| offsetY | 垂直偏移 | `number` | 0 |

### useDrop

Expand Down
11 changes: 11 additions & 0 deletions packages/hooks/src/useInfiniteScroll/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -348,4 +348,15 @@ describe('useInfiniteScroll', () => {
loadMore();
});
});

it('error result', async () => {
const { result } = setup(async () => {
throw new Error('error message');
});
await act(async () => {
jest.advanceTimersByTime(1000);
});

expect(result.current.error?.message).toBe('error message');
});
});
5 changes: 3 additions & 2 deletions packages/hooks/src/useInfiniteScroll/index.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,12 @@ const {
### Result

| Property | Description | Type |
| ------------- | ------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------ |
| data | The data returned by the service, where the `list` attribute is the aggregated data | `TData` \| `undefined` |
| ------------- | ------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------ | ----------- |
| data | The data returned by the service, where the `list` attribute is the aggregated data | `TData` \ | `undefined` |
| loading | Is the first request in progress | `boolean` |
| loadingMore | Is more data request in progress | `boolean` |
| noMore | Whether there is no more data, it will take effect after configuring `options.isNoMore` | `boolean` |
| error | Request error message | `Error` |
| loadMore | Load more data, it will automatically catch the exception, and handle it through `options.onError` | `() => void` |
| loadMoreAsync | Load more data, which is consistent with the behavior of `loadMore`, but returns Promise, so you need to handle the exception yourself | `() => Promise<TData>` |
| reload | Load the first page of data, it will automatically catch the exception, and handle it through `options.onError` | `() => void` |
Expand Down
3 changes: 2 additions & 1 deletion packages/hooks/src/useInfiniteScroll/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const useInfiniteScroll = <TData extends Data>(
return isNoMore(finalData);
}, [finalData]);

const { loading, run, runAsync, cancel } = useRequest(
const { loading, error, run, runAsync, cancel } = useRequest(
async (lastData?: TData) => {
const currentData = await service(lastData);
if (!lastData) {
Expand Down Expand Up @@ -122,6 +122,7 @@ const useInfiniteScroll = <TData extends Data>(
return {
data: finalData,
loading: !loadingMore && loading,
error,
loadingMore,
noMore,

Expand Down
5 changes: 3 additions & 2 deletions packages/hooks/src/useInfiniteScroll/index.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,12 @@ const {
### Result

| 参数 | 说明 | 类型 |
| ------------- | -------------------------------------------------------------------------- | ------------------------ |
| data | service 返回的数据,其中的 `list` 属性为聚合后数据 | `TData` \| `undefined` |
| ------------- | -------------------------------------------------------------------------- | ------------------------ | ----------- |
| data | service 返回的数据,其中的 `list` 属性为聚合后数据 | `TData` \ | `undefined` |
| loading | 是否正在进行首次请求 | `boolean` |
| loadingMore | 是否正在进行更多数据请求 | `boolean` |
| noMore | 是否没有更多数据了,配置 `options.isNoMore` 后生效 | `boolean` |
| error | 请求错误消息 | `Error` |
| loadMore | 加载更多数据,会自动捕获异常,通过 `options.onError` 处理 | `() => void` |
| loadMoreAsync | 加载更多数据,与 `loadMore` 行为一致,但返回的是 Promise,需要自行处理异常 | `() => Promise<TData>` |
| reload | 加载第一页数据,会自动捕获异常,通过 `options.onError` 处理 | `() => void` |
Expand Down
18 changes: 13 additions & 5 deletions packages/hooks/src/useMemoizedFn/index.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ nav:

# useMemoizedFn

Hooks for persistent functions. In theory, useMemoizedFn can be used instead of useCallback.
Hooks for persistent functions. In general, useMemoizedFn can be used instead of useCallback. See [FAQ](#faq) for special cases.

In some scenarios, we need to use useCallback to cache a function, but when the second parameter deps changes, the function will be regenerated, causing the function reference to change.

Expand Down Expand Up @@ -42,17 +42,25 @@ const func = useMemoizedFn(() => {
## API

```typescript
const fn = useMemoizedFn<T>(fn: T): T;
const memoizedFn = useMemoizedFn<T>(fn: T): T;
```

### Result

| Property | Description | Type |
| -------- | -------------------------------------- | ------------------------- |
| fn | Fn the reference address never changes | `(...args: any[]) => any` |
| Property | Description | Type |
| ---------- | ------------------------------------------------- | ------------------------- |
| memoizedFn | Function that the reference address never changes | `(...args: any[]) => any` |

### Params

| Property | Description | Type | Default |
| -------- | --------------------------------- | ------------------------- | ------- |
| fn | Function that require persistence | `(...args: any[]) => any` | - |

## FAQ

### The function returned by `useMemoizedFn` will not inherit properties from fn itself?

The function returned by `useMemoizedFn` is entirely different from the reference of the passed `fn`, and it does not inherit any properties from `fn` itself. If you want to preserve the properties of the function itself after memoization, `useMemoizedFn` currently does not fulfill that requirement. In this case, consider downgrading to using `useCallback` or `useMemo` instead.

Related issues: [2273](https://github.com/alibaba/hooks/issues/2273)
18 changes: 13 additions & 5 deletions packages/hooks/src/useMemoizedFn/index.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ nav:

# useMemoizedFn

持久化 function 的 Hook,理论上,可以使用 useMemoizedFn 完全代替 useCallback。
持久化 function 的 Hook,一般情况下,可以使用 useMemoizedFn 完全代替 useCallback,特殊情况见 [FAQ](#faq)

在某些场景中,我们需要使用 useCallback 来记住一个函数,但是在第二个参数 deps 变化时,会重新生成函数,导致函数地址变化。

Expand Down Expand Up @@ -42,17 +42,25 @@ const func = useMemoizedFn(() => {
## API

```typescript
const fn = useMemoizedFn<T>(fn: T): T;
const memoizedFn = useMemoizedFn<T>(fn: T): T;
```

### Result

| 参数 | 说明 | 类型 |
| ---- | ------------------------- | ------------------------- |
| fn | 引用地址永远不会变化的 fn | `(...args: any[]) => any` |
| 参数 | 说明 | 类型 |
| ---------- | -------------------------- | ------------------------- |
| memoizedFn | 引用地址永远不会变化的函数 | `(...args: any[]) => any` |

### Params

| 参数 | 说明 | 类型 | 默认值 |
| ---- | ---------------- | ------------------------- | ------ |
| fn | 需要持久化的函数 | `(...args: any[]) => any` | - |

## FAQ

### `useMemoizedFn` 返回的函数没有继承 fn 自身的属性?

`useMemoizedFn` 返回的函数与传入的 fn 的引用完全不同,且没有继承 fn 自身的属性。如果想要持久化后函数自身的属性不丢失,目前 `useMemoizedFn` 满足不了,请降级使用 `useCallback``useMemo`

Related issues: [2273](https://github.com/alibaba/hooks/issues/2273)
8 changes: 4 additions & 4 deletions packages/hooks/src/useVirtualList/index.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ const [list, scrollTo] = useVirtualList<T>(

### Params

| Property | Description | Type | Default |
| ------------ | ----------------------------------------------------- | --------- | ------- |
| originalList | The original list that contains a lot of data entries | `T[]` | `[]` |
| options | config | `Options` | - |
| Property | Description | Type | Default |
| ------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------- | --------- | ------- |
| originalList | The original list that contains a lot of data entries. **Attention: must undergo useMemo processing or never change, otherwise there will be a dead loop** | `T[]` | `[]` |
| options | config | `Options` | - |

### Options

Expand Down
8 changes: 4 additions & 4 deletions packages/hooks/src/useVirtualList/index.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ const [list, scrollTo] = useVirtualList<T>(

### Params

| 参数 | 说明 | 类型 | 默认值 |
| ------------ | ------------------ | --------- | ------ |
| originalList | 包含大量数据的列表 | `T[]` | `[]` |
| options | 配置项 | `Options` | - |
| 参数 | 说明 | 类型 | 默认值 |
| ------------ | ------------------------------------------------------------------------------ | --------- | ------ |
| originalList | 包含大量数据的列表**注意:必须经过 useMemo 处理或者永不变化,否则会死循环** | `T[]` | `[]` |
| options | 配置项 | `Options` | - |

### Options

Expand Down

0 comments on commit b251b7c

Please sign in to comment.