configcat / react-sdk

ConfigCat SDK for React. ConfigCat is a hosted feature flag service: https://configcat.com. Manage feature toggles across frontend, backend, mobile, desktop apps. Alternative to LaunchDarkly. Management app + feature flag SDKs.
MIT License
12 stars 7 forks source link

fix(Hook): Support re-evaluation on user change #30

Closed pdulich closed 1 year ago

pdulich commented 1 year ago

Introduce a new property to the useFeatureFlag hook to enable the re-evaluation of feature flags whenever the user changes.

Currently, the useFeatureFlag uses the user within a useEffect hook without specifying it as a dependency. This causes the useEffect hook to not re-trigger when the user changes. Because of this, the feature flag will still have the state based on the old user.

To fix this, the user could simply be added to the dependencies of the useEffect. But this could mean a breaking change: Callers may create a new user object on every re-render. Because the user is an object and not a simple string or the like, useEffect would re-render even if the contents of the user didn't change but only the instance. This will result in an infinite loop. Additionally, users may not expect this re-evaluation logic.

To not introduce this breaking change, don't make the re-evaluation the default. Instead, use a new optional property to enable this new logic. If wanted, this logic may be made the default in the future.

Fixes: #27

sonarcloud[bot] commented 1 year ago

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
No Duplication information No Duplication information