Skip to content

Pass dependencies to useMemo callback as arguments #14835

Closed
@davej

Description

@davej

Do you want to request a feature or report a bug?

Feature

What is the current behavior?

The useMemo factory function does not receive any arguments.

What is the desired behavior?

The useMemo factory function would receive the dependencies as arguments.

Why?

This would allow more compact syntax for memoizing components because of implicit returns and desctructuring. This came to mind after experiencing some of the issues in #14110. There may be other potential use cases too

Example of current behavior

const Avatar = () => {
  const [src] = useSomeGlobalState([
    state => state.user.avatar.src
  ]);
  return useMemo(() => <img src={src} />, [src])
}

Example of proposed behavior

const Avatar = () => 
  useMemo(
    (src) => <img src={src} />,
    useSomeGlobalState([state => state.user.avatar.src])
  );

Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?

React 16.8.1

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions