-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
A hook that returns previous value of given state.
- Loading branch information
Showing
9 changed files
with
220 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,5 @@ | ||
--- | ||
'@raddix/use-previous': major | ||
--- | ||
|
||
Added the usePrevious hook |
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,59 @@ | ||
--- | ||
title: usePrevious | ||
description: Returns previous value of given state. | ||
--- | ||
|
||
## Installation | ||
|
||
Install the custom hook from your command line. | ||
|
||
<Snippet pkg text='@raddix/use-previous' /> | ||
|
||
## Usage | ||
|
||
```tsx | ||
import { useState } from 'react'; | ||
import { usePrevious } from '@raddix/use-previous'; | ||
|
||
export default function App() { | ||
const [count, setCount] = useState(0) | ||
const previousCount = usePrevious(count); | ||
|
||
return ( | ||
<div> | ||
<p>Current value: {count}</p> | ||
<p>Previous value: {previousCount}</p> | ||
<button onClick={() => setCount(count + 1)}> | ||
Increment Count | ||
</button> | ||
</div> | ||
) | ||
} | ||
``` | ||
|
||
## API | ||
|
||
### Parameters | ||
|
||
<ApiTable | ||
data={[ | ||
{ | ||
name: 'value', | ||
type: 'any', | ||
description: 'The value of the state.', | ||
required: 'Yes' | ||
} | ||
]} | ||
/> | ||
|
||
### Returns | ||
|
||
<ApiTable | ||
data={[ | ||
{ | ||
name: 'previousValue', | ||
type: 'any', | ||
description: 'The previous value of given state.', | ||
} | ||
]} | ||
/> |
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,59 @@ | ||
--- | ||
title: usePrevious | ||
description: Devuelve el valor anterior del estado dado. | ||
--- | ||
|
||
## Instalación | ||
|
||
Instala el custom hook desde su linea de comando. | ||
|
||
<Snippet pkg text='@raddix/use-previous' /> | ||
|
||
## Uso | ||
|
||
```tsx | ||
import { useState } from 'react'; | ||
import { usePrevious } from '@raddix/use-previous'; | ||
|
||
export default function App() { | ||
const [count, setCount] = useState(0) | ||
const previousCount = usePrevious(count); | ||
|
||
return ( | ||
<div> | ||
<p>Valor actual: {count}</p> | ||
<p>Valor anterior: {previousCount}</p> | ||
<button onClick={() => setCount(count + 1)}> | ||
Increment Count | ||
</button> | ||
</div> | ||
) | ||
} | ||
``` | ||
|
||
## API | ||
|
||
### Parámetros | ||
|
||
<ApiTable | ||
data={[ | ||
{ | ||
name: 'value', | ||
type: 'any', | ||
description: 'El valor del estado.', | ||
required: 'Si' | ||
} | ||
]} | ||
/> | ||
|
||
### Devuelve | ||
|
||
<ApiTable | ||
data={[ | ||
{ | ||
name: 'previousValue', | ||
type: 'any', | ||
description: 'El valor anterior del estado dado.', | ||
} | ||
]} | ||
/> |
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,5 @@ | ||
# usePrevious | ||
|
||
A hook that returns previous value of given state. | ||
|
||
Please refer to the [documentation](https://raddix.dev/hooks/use-previous) for more information. |
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,46 @@ | ||
{ | ||
"name": "@raddix/use-previous", | ||
"description": "A hook that returns previous value of given state.", | ||
"version": "0.1.0", | ||
"license": "MIT", | ||
"main": "src/index.ts", | ||
"author": "Moises Machuca Valverde <rolan.machuca@gmail.com> (https://www.moisesmachuca.com)", | ||
"homepage": "https://raddix.dev/hooks/use-previous", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/gdvu/raddix.git" | ||
}, | ||
"keywords": [ | ||
"react-hook", | ||
"react-previous-hook", | ||
"react-use-previous", | ||
"use-previous", | ||
"use-previous-hook", | ||
"hook-previous" | ||
], | ||
"sideEffects": false, | ||
"scripts": { | ||
"lint": "eslint \"{src,tests}/*.{ts,tsx,css}\"", | ||
"clean": "rm -rf .turbo && rm -rf node_modules && rm -rf dist", | ||
"build": "tsup src --dts", | ||
"prepack": "clean-package", | ||
"postpack": "clean-package restore" | ||
}, | ||
"files": [ | ||
"dist", | ||
"README.md" | ||
], | ||
"peerDependencies": { | ||
"react": ">=16.8.0", | ||
"react-dom": ">=16.8.0" | ||
}, | ||
"clean-package": "../../../clean-package.config.json", | ||
"tsup": { | ||
"clean": true, | ||
"target": "es2019", | ||
"format": [ | ||
"cjs", | ||
"esm" | ||
] | ||
} | ||
} |
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 const usePrevious = <T>(value: T): T | undefined => { | ||
const previous = useRef<T>(); | ||
|
||
useEffect(() => { | ||
previous.current = value; | ||
}, [value]); | ||
|
||
return previous.current; | ||
}; |
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,22 @@ | ||
import { renderHook } from '@testing-library/react'; | ||
import { usePrevious } from '../src'; | ||
|
||
describe('usePrevious test:', () => { | ||
it('should returns undefined on initial render', () => { | ||
const { result } = renderHook(() => usePrevious(0)); | ||
|
||
expect(result.current).toBeUndefined(); | ||
}); | ||
|
||
it('should return previous value', () => { | ||
const { result, rerender } = renderHook(state => usePrevious(state), { | ||
initialProps: 0 | ||
}); | ||
|
||
rerender(1); | ||
expect(result.current).toBe(0); | ||
|
||
rerender(2); | ||
expect(result.current).toBe(1); | ||
}); | ||
}); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.