fix: react context no longer changes on flag change#1081
fix: react context no longer changes on flag change#1081
Conversation
|
@launchdarkly/js-sdk-common size report |
|
@launchdarkly/browser size report |
|
@launchdarkly/js-client-sdk size report |
|
@launchdarkly/js-client-sdk-common size report |
5936ae2 to
baed8fb
Compare
packages/sdk/react-native/src/hooks/variation/useTypedVariation.ts
Outdated
Show resolved
Hide resolved
packages/sdk/react-native/src/hooks/variation/useTypedVariation.ts
Outdated
Show resolved
Hide resolved
152a584 to
2086a07
Compare
packages/sdk/react-native/src/hooks/variation/useTypedVariation.ts
Outdated
Show resolved
Hide resolved
4d4550f to
5669d5d
Compare
4dae4ab to
eba484e
Compare
| setValue(newValue); | ||
| } | ||
| }; | ||
| ldClient.on(`change:${key}`, handleChange); |
There was a problem hiding this comment.
I am tempted to say we should add a listener for context changes as well. Which then trigger a re-evaluation, but won't end up changing state if the value is the same.
I was doing some testing in flutter and that ended up being the sweet spot with providers to ensure that each context appeared correctly in an audience.
There was a problem hiding this comment.
I think this is reasonable, but that might alter the API here since the original hook would only return the flag value. Not sure if it would be confusing if a state update is dispatched on context change without being able to get the actual changed value?
We can introduce a new hook for the context value (or say to use the react context with getContext()). Don't think there is a really good answer to this, honestly. You will have potentially varying behaviors depending on the flag value type because I think react will not dispatch an update if the setState is called on a value that is equivalent to the previous state value.
Requirements
Related issues
sdk-1792
Describe the solution you've provided
Describe alternatives you've considered
I think this change may require a major version, but we can probably avoid that with some compromises.
Additional context
There is a testing gap here and I will create some tasks to address. I want to do this in a different PR because I would like to refactor RN unit test to use https://oss.callstack.com/react-native-testing-library/ instead of the react testing library.
We also need to revisit the detox test, from some research it seems like detox is the correct technology for e2e testing, but we may want to revisit how to plug in to the sdk test harness (and deprecate the example testing)
Note
Medium Risk
Changes core flag-evaluation hooks to manage subscriptions and state updates; incorrect listener cleanup or dependency handling could cause missed updates or leaks, though the change is localized to the React Native SDK.
Overview
Prevents flag updates from changing the React context value by removing provider-level change listeners and memoizing the
{ client }context value inLDProvider(deletingsetupListeners).Adds per-flag subscriptions in
useTypedVariationanduseTypedVariationDetailthat initialize local state, listen onchange:${key}, and update only when the evaluated value/detail actually changes (usingfastDeepEqual), rather than relying on provider re-renders. Tests are updated accordingly, including removal of the now-obsoletesetupListenersunit test.Written by Cursor Bugbot for commit eba484e. This will update automatically on new commits. Configure here.