1.1.1 • Published 2 years ago

use-last v1.1.1

Weekly downloads
8
License
MIT
Repository
github
Last release
2 years ago

use-last

Stable release

A React hook to conditionally return the last value that has met a certain criteria.

This is useful when you have a React component that has a state or prop that changes over time, but you want to conditionally skip some values.

Example

Implementation:

import useLast from 'use-last';

function LastValue({value}) {
  const lastDefinedValue = useLast(value);
  return <p>{lastDefinedValue}</p>;
}

Usage:

// Renders "2"
<LastValue value={2} />

// Still renders "2"
<LastValue value={undefined} />

// Renders "4"
<LastValue value={4} />

Configuration

By default, value is checked for being !== undefined. You can provide a second argument to customize this condition:

import useLast from 'use-last';

function LastEvenValue({value}) {
  const lastEvenValue = useLast(value, value % 2 === 0);
  return <p>{lastEvenValue}</p>;
}
1.1.1

2 years ago

1.1.0

2 years ago

1.0.1

3 years ago

1.0.0

3 years ago

0.1.0

3 years ago