Skip to content

[BUG] CommerceApiProvider context causes unnecessary re-renders of the application #2592

@narcissichka

Description

@narcissichka

Summary

CommerceApiProvider passes a new object each time into the context as 'value'. Using why-did-you-render package, I have identified that each time I use useCommerceApi hook in my code to get the context, the application re-renders unnecessarily (for nearly 5 times).
It happens because of the error: 'different objects that are equal by value.'. Which means that CommerceApiProvider context value is not memoized.

Expected result

The value object that is passed to context provider should always be memoized to avoid unnecessary re-renders because of the object reference changes.

Actual result

The value object is re-created on every render, which causes all the parts of application that use the context to re-render.

Additional information

The place in code that causes the problem:

Image

The problem representation (objects are equal by value, but different by reference):

Image Image

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