Skip to content

[hook]: Create useCustomCompareEffect hook #507

Description

@ashenoooone

React compares dependencies with Object.is, so an object or an array rebuilt during
render always counts as changed and the effect fires on every render. Filter objects,
option bags, query params: anything built inline hits this.

The library has one hook for that, useShallowEffect, and it does not do what its name
says. Its comparator recurses into nested values, so it is a deep compare. There is no
shallow compare, no honestly named deep one, and no way to pass your own rule for cases
where neither fits, like re-running only when user.id changes.

Proposed: useCustomCompareEffect(effect, deps, comparator) as the base hook, with
useShallowEffect and useDeepEffect as one-line wrappers over it, so the dependency
bookkeeping lives in one place. The two comparison functions become public helpers.

useCustomCompareEffect(() => subscribe(user), [user], ([user], [prev]) => user.id === prev.id);
useShallowEffect(() => console.log('effect'), [filter]);
useDeepEffect(() => console.log('effect'), [filter]);

Fixing useShallowEffect changes its behavior, so it needs a release note.

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