Skip to content

[Hooks] Alternative useCallback implementation #83

Description

@edkalina

According to this pageuseCallback(fn, inputs) is equivalent to useMemo(() => fn, inputs)

I suggest another one (implemented with current hooks):

functionuseHandler(handler){constholder=useRef();holder.current=handler;// TODO: avoid useMemo?returnuseMemo(()=>function(...args){returnholder.current.apply(this,args);},[]);}

It has advantages:

  • no need to specify inputs, latest instance of handler will be called
  • result of useHandler is constant for all rerenders

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions