Skip to content

docs(usePrevious): update shouldUpdate default value #2122

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion packages/hooks/src/usePrevious/__tests__/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { renderHook } from '@testing-library/react';
import usePrevious, { ShouldUpdateFunc } from '../';
import type { ShouldUpdateFunc } from '../';
import usePrevious from '../';

describe('usePrevious', () => {
function getHook<T>(initialValue?: T, compareFunction?: ShouldUpdateFunc<T>) {
Expand Down Expand Up @@ -35,6 +36,15 @@ describe('usePrevious', () => {
expect(hook.result.current).toBe(4);
});

it('should not update previous value if current value is the same', () => {
const hook = getHook(0);
expect(hook.result.current).toBeUndefined();
hook.rerender({ val: 1 });
expect(hook.result.current).toBe(0);
hook.rerender({ val: 1 });
expect(hook.result.current).toBe(0);
});

it('should work fine with `undefined` values', () => {
const hook = renderHook(({ value }) => usePrevious(value), {
initialProps: { value: undefined as undefined | number },
Expand Down
8 changes: 4 additions & 4 deletions packages/hooks/src/usePrevious/index.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const previousState: T = usePrevious<T>(

### Params

| Property | Description | Type | Default |
| ------------ | ------------------------------------------------------------- | -------------------------------------------- | ------------------- |
| state | The state that needs to be tracked | `T` | - |
| shouldUpdate | Optional. Customize whether the state value should be updated | `(prev: T \| undefined, next: T) => boolean` | `(a, b) => a !== b` |
| Property | Description | Type | Default |
| ------------ | ------------------------------------------------------------- | -------------------------------------------- | ---------------------------- |
| state | The state that needs to be tracked | `T` | - |
| shouldUpdate | Optional. Customize whether the state value should be updated | `(prev: T \| undefined, next: T) => boolean` | `(a, b) => !Object.is(a, b)` |
8 changes: 4 additions & 4 deletions packages/hooks/src/usePrevious/index.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const previousState: T = usePrevious<T>(

### Params

| 参数 | 说明 | 类型 | 默认值 |
| ------------ | -------------------------- | -------------------------------------------- | ------------------- |
| state | 需要记录变化的值 | `T` | - |
| shouldUpdate | 可选,自定义判断值是否变化 | `(prev: T \| undefined, next: T) => boolean` | `(a, b) => a !== b` |
| 参数 | 说明 | 类型 | 默认值 |
| ------------ | -------------------------- | -------------------------------------------- | ---------------------------- |
| state | 需要记录变化的值 | `T` | - |
| shouldUpdate | 可选,自定义判断值是否变化 | `(prev: T \| undefined, next: T) => boolean` | `(a, b) => !Object.is(a, b)` |