usePrevious

Syntax

usePrevious<T>(value: T): T

The usePrevious hook allows you to keep track of the previous value of something. This can be especially useful if you need to identify whether a value changed and what the difference would be.

Noticing changing properties

function useOpaqueLabel({ opacity }) {
    const oldOpacity = usePrevious(opacity);
    const opaqueElement = useRef(null);
    console.log('Opacity changed from %s to %s', oldOpacity, opacity);
    return {
        opacity,
        opaqueElement
    };
}

Last updated

Was this helpful?