Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: Ability to filter consumers in context #20014

Closed
Plitzi opened this issue Oct 14, 2020 · 3 comments
Closed

Feature Request: Ability to filter consumers in context #20014

Plitzi opened this issue Oct 14, 2020 · 3 comments
Labels
Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug

Comments

@Plitzi
Copy link

Plitzi commented Oct 14, 2020

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 ?

  • 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

@Plitzi Plitzi added the Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug label Oct 14, 2020
@bvaughn
Copy link
Contributor

bvaughn commented Oct 14, 2020

What you're describing could be implemented with a wrapper component and React.memo, no?

Maybe something like this:

function createFilteredConsumer(context, selectorFn, Component) {
  function areEqual(prevProps, nextProps) {
    const prevContextValue = prevProps.contextValue;
    const nextContextValue = nextProps.contextValue;

    // Hook into selector function logic here...
  }

  const MemoizedComponent = React.memo(Component, areEqual);

  function Wrapper(props) {
    const contextValue = 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-debugging
    const name = Component.displayName || Component.name || "Component";
    Wrapper.displayName = `FilteredConsumer(${name})`;
  }

  // Wrap with React.forwardRef() if needed.
  return Wrapper;
}

@markerikson
Copy link
Contributor

This is the intent behind the "context selectors" RFC and PR:

@bvaughn
Copy link
Contributor

bvaughn commented Oct 15, 2020

Thanks for the links, Mark.

Going to close this issue.

@bvaughn bvaughn closed this as completed Oct 15, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug
Projects
None yet
Development

No branches or pull requests

2 participants