An open API service indexing awesome lists of open source software.

https://github.com/donavon/use-prev-prop

A custom React Hook that maintains a previous value for a prop (or any other value).
https://github.com/donavon/use-prev-prop

hooks props react reactjs

Last synced: 9 months ago
JSON representation

A custom React Hook that maintains a previous value for a prop (or any other value).

Awesome Lists containing this project

README

          

# @use-it/prev-prop
[![npm version](https://badge.fury.io/js/%40use-it%2Fprev-prop.svg)](https://badge.fury.io/js/%40use-it%2Fprev-prop)
[![All Contributors](https://img.shields.io/badge/all_contributors-3-orange.svg?style=flat-square)](#contributors)

A custom React Hook that maintains a previous value for a prop (or any other value).
Based on [a tweet](https://twitter.com/chrisachard/status/1177308524877144065)
by [Chris Achard](https://github.com/chrisachard).

## Installation

```bash
$ npm i @use-it/prev-prop
```

or

```bash
$ yarn add @use-it/prev-prop
```

## Usage

Here is a basic setup.

```js
const previousProp = usePrevProp(prop);
```

### Parameters

Here are the parameters that you can use.

| Parameter | Description |
| :--------- | :------------------------------------------------------------------------------- |
| `prop` | The current prop |

### Return

This hook returns the previous value of the prop. Initially, this value will be `undefined`.

## Example

Let's look at some sample code. Here is a `Count` component that displays the count and the previous count.

```js
import React from 'react';
import usePrevProp from '@use-it/prev-prop';

const Count = ({ count }) => {
const prevCount = usePrevProp(count);

return (


Current Count: {count}

Prev Count: {prevCount}


);
};

export default Count;
```

## License

**[MIT](LICENSE)** Licensed

## Contributors ✨

Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)):


Donavon West
Donavon West

🚇 ⚠️ 💡 🤔 🚧 👀 🔧 💻
chrisachard
chrisachard

🤔
Leza Morais Lutonda
Leza Morais Lutonda

💻

This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome!