You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat: Support for providing custom contexts (#313)
**Requirements**
- [X] I have added test coverage for new or changed functionality
- [X] I have followed the repository's [pull request submission
guidelines](../blob/main/CONTRIBUTING.md#submitting-pull-requests)
- [ ] 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:
```javascript
<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**
Co-authored-by: Ryan Lamb <4955475+kinyoklion@users.noreply.github.com>
0 commit comments