You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When we use contexts with provider/consumer, this is a good feature but in some scenarios you just want to trigger the consumer in specific conditions
This ticket is about to request a feature where we can have the ability to define filters in our consumer, and based on these filters the consumer will be triggered or not when the provider changes the value
Why ?
Performance boost (because this only will re-render the consumer based on the filters)
Usability (we can design better apps and decrease the unnecesary re-renders)
Example
i have a context to manage elements, but i just want to re-render based on the ID
The text was updated successfully, but these errors were encountered:
What you're describing could be implemented with a wrapper component and React.memo, no?
Maybe something like this:
functioncreateFilteredConsumer(context,selectorFn,Component){functionareEqual(prevProps,nextProps){constprevContextValue=prevProps.contextValue;constnextContextValue=nextProps.contextValue;// Hook into selector function logic here...}constMemoizedComponent=React.memo(Component,areEqual);functionWrapper(props){constcontextValue=React.useContext(Context);return<MemoizedComponent{...props}contextValue={contextValue}/>;}if(__DEV__){// For easier debugging.// https://reactjs.org/docs/higher-order-components.html#convention-wrap-the-display-name-for-easy-debuggingconstname=Component.displayName||Component.name||"Component";Wrapper.displayName=`FilteredConsumer(${name})`;}// Wrap with React.forwardRef() if needed.returnWrapper;}
Good Afternoon,
I would like to suggest this:
When we use contexts with provider/consumer, this is a good feature but in some scenarios you just want to trigger the consumer in specific conditions
This ticket is about to request a feature where we can have the ability to define filters in our consumer, and based on these filters the consumer will be triggered or not when the provider changes the value
Why ?
Example
i have a context to manage elements, but i just want to re-render based on the ID
The text was updated successfully, but these errors were encountered: