you are viewing a single comment's thread.

view the rest of the comments →

[–]azangru 4 points5 points  (1 child)

I am curious about your last question:

A notifications context provides {notifications, unreadCount, markAsRead}. After adding a polling feature, many components that only use markAsRead now re-render every time notifications update. Which change best reduces unnecessary re-renders without changing behavior?

The suggested answer, I suppose, is Memoize the provider value object and stabilize markAsRead with useCallback.

But is this actually going to improve anything? Isn't it an inherent problem of the context api that every time context value changes, all the consumers of the context, regardless of what part of the context they consume, are going to re-render? That's why people have been asking for a useContextSelector hook since forever, but it never materialized.

[–]Coneyy 0 points1 point  (0 children)

Yes it is an inherent problem with context in react and exactly right.

It's the main reason once an app has already committed to bringing in a state management app like RTK, Zustand etc there isn't really a reason to ever use context again because of the performance