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

feat: Support for providing custom contexts #313

Merged
merged 2 commits into from
Nov 1, 2024

Conversation

drewpc
Copy link
Contributor

@drewpc drewpc commented Sep 5, 2024

Requirements

  • I have added test coverage for new or changed functionality
  • 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 drewpc requested a review from a team as a code owner September 5, 2024 08:02
@kinyoklion
Copy link
Member

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

@drewpc
Copy link
Contributor Author

drewpc commented Oct 1, 2024

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

@kinyoklion
Copy link
Member

Hello @drewpc,

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

Thank you,
Ryan

@kinyoklion kinyoklion changed the title Enable support for providing custom contexts feat: Support for providing custom contexts Nov 1, 2024
@kinyoklion kinyoklion merged commit a8b52f4 into launchdarkly:main Nov 1, 2024
1 check passed
kinyoklion added a commit that referenced this pull request Nov 4, 2024
🤖 I have created a release *beep* *boop*
---


##
[3.6.0](launchdarkly-react-client-sdk-v3.5.0...launchdarkly-react-client-sdk-v3.6.0)
(2024-11-01)


### Features

* Support for providing custom contexts
([#313](#313))
([a8b52f4](a8b52f4))
* **typescript:** export the LDProps interface for access in application
code
([#321](#321))
([7a084c5](7a084c5))

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Ryan Lamb <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants