Skip to content

[Feature]: Allow partial selectors for react-context-selector #32132

@seanpmaxwell

Description

@seanpmaxwell

Library

React Components / v9 (@fluentui/react-components)

Describe the feature that you would like added

Coming from a redux environment, I was used to doing a lot of this:

  const {
    session,
    signupMethodStr,
  } = useAppCtx(ctx => ({
    session: ctx.session,
    signupMethodStr: ctx.session.signupMethodStr,
  }));

Which is a lot nicer/cleaner that having to call the useAppCtx() hook multiple times. This causes a too many re-renders error in ReactJS? error though. I believe it's because new object gets created every time the selector is run. In order to fix this I had to do something hacky with the selector function:

export const useAppCtx = <T extends {}>(selector: TSlctr<T>): T => {
  const ctx = useContextSelector(AppCxt, ctx => ctx);
  return selector(ctx);
};

Instead of how the docs show:

export const useAppCtx = <T extends {}>(selector: TSlctr<T>): T => {
  return useContextSelector(AppCxt, selector);
};

Is there any chance we get could this to work by default? Thanks.

Have you discussed this feature with our team

No

Additional context

No response

Validations

  • Check that there isn't already an issue that request the same feature to avoid creating a duplicate.

Priority

None

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions