Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[@mantine/hooks] use-previous: Init hook (#2313)
- Loading branch information
Showing
4 changed files
with
61 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
--- | ||
group: 'mantine-hooks' | ||
package: '@mantine/hooks' | ||
category: 'state' | ||
title: 'use-previous' | ||
order: 1 | ||
slug: /hooks/use-previous/ | ||
description: 'Get the previous value of a state' | ||
import: "import { usePrevious } from '@mantine/hooks';" | ||
docs: 'hooks/use-previous.mdx' | ||
source: 'mantine-hooks/src/use-previous/use-previous.ts' | ||
--- | ||
|
||
## Usage | ||
|
||
`use-previous` hook stores the previous value of a state in a ref. Returns undefined on initial render and the previous value of a state after rerender. | ||
|
||
Arguments: | ||
|
||
- `value`: The value to store in the ref. | ||
|
||
Returns: | ||
|
||
- `previousValue`: The previous value of the state. | ||
|
||
## Definition | ||
|
||
```tsx | ||
function usePrevious<T>(value: T): T | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { renderHook } from '@testing-library/react'; | ||
import { usePrevious } from './use-previous'; | ||
|
||
describe('@mantine/hooks/use-previous', () => { | ||
it('returns undefined on intial render', () => { | ||
const hook = renderHook(() => usePrevious(1)); | ||
expect(hook.result.current).toBeUndefined(); | ||
}); | ||
|
||
it('returns the previous value after update', () => { | ||
const hook = renderHook(({ state }) => usePrevious(state), { initialProps: { state: 1 } }); | ||
|
||
hook.rerender({ state: 2 }); | ||
expect(hook.result.current).toBe(1); | ||
|
||
hook.rerender({ state: 4 }); | ||
expect(hook.result.current).toBe(2); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { useEffect, useRef } from 'react'; | ||
|
||
export function usePrevious<T>(value: T): T | undefined { | ||
const ref = useRef<T>(); | ||
|
||
useEffect(() => { | ||
ref.current = value; | ||
}, [value]); | ||
|
||
return ref.current; | ||
} |