Skip to content
GaudiGaudi Design System home

Search documentation

Search every page, heading and paragraph.

usePrevious

The value a prop or state held before it last changed.

HooksState@gaudi/design-system/hooks/state/use-previousView in StorybookSource

For "went up / went down" indicators, transition direction and change detection. Tracked in state, so it is safe under concurrent rendering, and unchanged re-renders keep the same previous value.

Usage

import { usePrevious } from '@gaudi/design-system/hooks';
const previous = usePrevious(count);const trend = previous === undefined ? 'flat' : count > previous ? 'up' : 'down';

Examples

Change since the last value

Storybook

API

2 options
1 return value

Accessibility

When a change direction is shown with colour or an arrow, also state it in text ("up 12%").

Notes

  • Comparison uses Object.is; pass primitives or stable objects.