-
Notifications
You must be signed in to change notification settings - Fork 45
fix: memoize React client to provide stability #1276
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
Conversation
Signed-off-by: Matt Cowley <[email protected]>
Summary of ChangesHello @MattIPv4, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request addresses a critical stability issue within the React OpenFeature provider. Previously, when a domain was passed instead of an explicit client, the client instance was inadvertently recreated on every re-render, leading to an unstable client. The core change introduces memoization to ensure a consistent client instance, thereby improving performance and predictability for components consuming the OpenFeature client. Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request correctly addresses a performance issue by memoizing the OpenFeature client instance within the OpenFeatureProvider. This prevents the client from being recreated on every render, providing stability for downstream hooks like useOpenFeatureClient. The change is well-implemented. I've added one suggestion to further enhance performance by also memoizing the context provider's value object, which would prevent unnecessary re-renders for all context consumers.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh wow, great catch. I wonder if it's worth adding a test to ensure we get the same client after a rerender.
|
Great find and fix! Can you add a test? You can probably just assert the object refs are equal. Try to make sure the test fails before your change. |
|
Yeh, can do, give me a bit to actually get this setup locally (I did this patch via GitHub's UI). |
|
This is probably causing some memory/handler leaks. Maybe hard to spot because of the nature of frontend, but it almost certainly must be. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a good catch, thanks!
Generally the recreation would be fine, but the handlers are the one thing that leaks.
Signed-off-by: MattIPv4 <[email protected]>
## This PR Failure first appeared in https://github.com/open-feature/js-sdk/actions/runs/18692818359/job/53302526714 -- noticed it locally while adding the test in #1276, and then saw the failure on main when my PR was merged. Signed-off-by: MattIPv4 <[email protected]>
🤖 I have created a release *beep* *boop* --- ## [1.0.2](react-sdk-v1.0.1...react-sdk-v1.0.2) (2025-10-31) ### 🐛 Bug Fixes * memoize React client to provide stability ([#1276](#1276)) ([405d61d](405d61d)) ### 🧹 Chore * mention debounce hook in react/ng docs ([#1272](#1272)) ([27666b8](27666b8)) --- This PR was generated with [Release Please](https://github.com/googleapis/release-please). See [documentation](https://github.com/googleapis/release-please#release-please). Signed-off-by: OpenFeature Bot <[email protected]>
This PR
👋 Maybe I'm missing something, but
useOpenFeatureClientcurrently returns an unstableclientthat is recreated every time the provider re-renders, if you pass a domain into the provider instead of a specific client.This PR wraps the creation of that client when a domain is passed in
useMemo, which should ensure that it is then stable for downstream usage via theuseOpenFeatureClienthook.As an example, due to
clientbeing unstable (combined with theshouldRunNowbehaviour in the client), this logsreadyevery time the provider re-renders rather than just once when the client is actually ready.The closest workaround I've found currently is checking if the domain matches w/
useState+useEffect: