@use-it/prev-prop
TypeScript icon, indicating that this package has built-in type declarations

0.1.0 • Public • Published

@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

Package Sidebar

Install

npm i @use-it/prev-prop

Weekly Downloads

28

Version

0.1.0

License

MIT

Unpacked Size

4.83 kB

Total Files

7

Last publish

Collaborators

  • donavon