0.1.0 • Published 6 years ago

@use-it/prev-prop v0.1.0

Weekly downloads
108
License
MIT
Repository
github
Last release
6 years ago

@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.

ParameterDescription
propThe 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