Playground

usePrevious

The value from the previous render, for comparing a change.

  • No DOM output.

Import

import '@monoline/css';
import {usePrevious} from '@monoline/react';

Not installed yet? Pick a route.

Copies the source and its styles into your project.

npx monoline add use-previous

Usage

import {useState} from 'react';
import {Button, usePrevious} from '@monoline/react';

function PreviousDemo() {
  const [value, setValue] = useState(0);
  const previous = usePrevious(value);
  return (
    <div>
      <Button variant="line" onClick={() => setValue((v) => v + 1)}>Increase</Button>
      <p>Now {value}, before {previous ?? 'nothing'}.</p>
    </div>
  );
}

<PreviousDemo />

Examples

Previous value

Behaviour

  • No DOM output.