@use-it/prev-prop

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

Usage no npm install needed!

<script type="module">
  import useItPrevProp from 'https://cdn.skypack.dev/@use-it/prev-prop';
</script>

README

@use-it/prev-prop

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

npm version

Installation

$ npm i @use-it/prev-prop

or

$ yarn add @use-it/prev-prop

Usage

Here is a basic setup.

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.

Example

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

import React, { useState } from 'react';
import usePrevProp from '@use-it/prev-prop';

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

  return (
    <div>
      <div>Current Count: {count}</div>
      <div>Prev Count: {prevCount}</div>
    </div>
  );
};

export default Count;

License

MIT Licensed