react-client-sdk icon indicating copy to clipboard operation
react-client-sdk copied to clipboard

Enable support for providing custom contexts

Open drewpc opened this issue 1 year ago • 3 comments

Requirements

  • [X] I have added test coverage for new or changed functionality
  • [X] I have followed the repository's pull request submission guidelines
  • [ ] I have validated my changes against all supported platform versions

How do I validate my changes against all supported platform versions?

Related issues

#127

Describe the solution you've provided

This PR enables developers to provide a custom React Context to the React SDK along-side a pre-initialized LDClient object. This allows applications to create one client per environment and/or project and create a unique React Context that corresponds with each client.

Also, in a microfrontend situation where multiple independent React applications are loaded on the same page, this custom React Context feature would allow a parent application to create the client and associated React Context. Then any child application loaded on the page could leverage those existing client and context objects.

Here's an example of how the React Contexts can be used together:

      <LDProvider1>
        <LDProvider2>
          <CustomContext1.Consumer>
            {({ flags }) => {
              return (
                <>
                  <span>consumer 1, flag 1 is {safeValue(flags.context1TestFlag)}</span>
                  <span>consumer 1, flag 2 is {safeValue(flags.context2TestFlag)}</span>
                </>
              );
            }}
          </CustomContext1.Consumer>
          <CustomContext2.Consumer>
            {({ flags }) => {
              return (
                <>
                  <span>consumer 2, flag 1 is {safeValue(flags.context1TestFlag)}</span>
                  <span>consumer 2, flag 2 is {safeValue(flags.context2TestFlag)}</span>
                </>
              );
            }}
          </CustomContext2.Consumer>
        </LDProvider2>
      </LDProvider1>

Describe alternatives you've considered

I am unaware of alternatives to providing a custom React context into the library because this library creates its own context at runtime.

Additional context

drewpc avatar Sep 05 '24 08:09 drewpc

Hello @drewpc,

Thank you for the contribution!

This appears to be a worthwhile capability. I'll be adding a task so we can schedule reviewing this as it is going to take some level of consideration.

Thank you, Ryan

kinyoklion avatar Sep 05 '24 17:09 kinyoklion

@kinyoklion checking in to see if there are any updates on this.

drewpc avatar Oct 01 '24 17:10 drewpc

Hello @drewpc,

No updates at the moment, but we are still planning on including it.

Thank you, Ryan

kinyoklion avatar Oct 01 '24 17:10 kinyoklion