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] React SDK: Provide hook to set relevant context "useContextMutator" #968

Open
toddbaert opened this issue Jun 28, 2024 · 3 comments · May be fixed by open-feature/react-test-app#9 or #1031
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed

Comments

@toddbaert
Copy link
Member

toddbaert commented Jun 28, 2024

Background

The OpenFeatureProvider React context provider binds an OpenFeature client to a React Context. Components within that boundary use the associated client for evaluations, etc. The client can be associated with a domain (see https://github.com/open-feature/js-sdk/blob/main/packages/react/README.md#multiple-providers-and-domains and https://github.com/open-feature/js-sdk/blob/main/packages/client/README.md#domains)

We do not currently have a hook that makes it easy to update this context. It's necessary to use the global object to do this. We should add a hook that allows the correct context for the enclosed scope to be set.

For example, this current implementation:

<OpenFeatureProvider domain={'my-domain'}>
  <button
    onClick={() => {
      OpenFeature.setContext('my-domain', updatedContext);
    }}
  >
      click here
  </button>
</OpenFeatureProvider>

Could be instead be:

<OpenFeatureProvider domain={'my-domain'}>
  <button
    onClick={() => {
      mutateContext(updatedContext); // function returned by new hook, associated with the 'my-domain' domain
    }}
  >
      click here
  </button>
</OpenFeatureProvider>

This would make things easier for users, and fit better into react idioms. Please test your solution but using it in the sample react app here.

Requirements

  • new hook added which returns function to mutate context (please come up with a reasonable name for the hook and returned function)
  • associated tests
  • associated README updates
  • React demo updated here
  • returned func(s) should probably be async

Additional Considerations

I'd love to see considerations from the implementor on the following:

  • should we export a couple different functions? setContext, mergeContext etc, which merge the context?
  • should we ignore changes if the reference hasn't changed?
@toddbaert toddbaert added enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed labels Jun 28, 2024
@toddbaert toddbaert changed the title [FEATURE] (React SDK) Provide hook to set relevant context useContextMutator [FEATURE] (React SDK) Provide hook to set relevant context "useContextMutator" Jun 28, 2024
@toddbaert toddbaert changed the title [FEATURE] (React SDK) Provide hook to set relevant context "useContextMutator" [FEATURE] React SDK: Provide hook to set relevant context "useContextMutator" Jun 28, 2024
@wichopy
Copy link

wichopy commented Oct 6, 2024

volunteer to tackle this one

@wichopy
Copy link

wichopy commented Oct 6, 2024

@toddbaert is there tooling to pull in the build with my changes in js sdk and use it with react test app project?

I was able to locally test manually using yalc but was wondering how you would go about it.

@beeme1mr
Copy link
Member

beeme1mr commented Oct 7, 2024

Yalc is probably the easier way to try this in the test app.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
3 participants