Melody Documentation
  • Why Melody?
  • Quickstart
    • What is Melody?
    • Tutorial
    • Installation
  • Templates
    • Twig
    • Mounting Components
    • Keys and Loops
    • Attributes
    • CSS Classes
    • Inline Styles
    • Refs
    • CSS Modules
  • Melody-Streams
    • Basic concept
    • A Step-by-Step Walkthrough
  • MELODY-STREAMS API
    • createComponent
    • The component function
  • melody-hooks
    • Overview
    • Rules of Hooks
    • Event Handling
    • Separation of Concerns
  • melody-hooks API
    • useState
    • useAtom
    • useEffect
    • useRef
    • useCallback
    • useMemo
    • useReducer
    • usePrevious
  • melody-component
    • Overview
  • melody-hoc
    • Overview
Powered by GitBook
On this page
  • Syntax
  • Noticing changing properties

Was this helpful?

  1. melody-hooks API

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.

This hook is not part of React and even we are not entirely sure whether this is actually useful. You can see that by looking at the very uninspired example below.

The current assumption is that you should avoid using this hook.

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
    };
}
PrevioususeReducerNextOverview

Last updated 5 years ago

Was this helpful?