Skip to content

[FEATURE] React SDK: Provide hook to set relevant context "useContextMutator" #968

@toddbaert

Description

@toddbaert

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions