Skip to content

Optimization in use-page-errors resetPageErrors function #54

Description

@brandongregoryscott

In a recent debugging session on a client project, we found that some unnecessary state changes were being kicked off by the use-page-errors hook, specifically the resetPageErrors function that is exported. Add a simple check with the callback version of setPageErrors to check to see if the previous state has values - if it doesn't, there's no need to set the state and cause a re-render to consuming components (due to referential inequality of arrays).

Before:

const resetPageErrors = useCallback(() => {
    setPageErrors([]);
}, []);

After:

const resetPageErrors = useCallback(() => {
    setPageErrors((prevState) =>
        CollectionUtils.hasValues(prevState) ? [] : prevState
    );
}, []);

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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions