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
fix(hooks): Return latest decision values on first call, and re-render in response to decision input changes (#64)
Summary:
This PR addresses two issues with the useExperiment and useFeature hooks:
- Changes to the overrides argument do not trigger a re-render
- Even after the client instance is ready, the first call returns false (for feature enabled status from useFeature) or null (for variation assignment from useExperiment).
A boolean-returning isReady method is added to ReactSDKClient. When isReady returns true, useFeature and useExperiment don't wait for the fulfillment of the onReady promise before calling their respective decision methods.
Arguments that affect decision results (experiment/feature key + overrides) are tracked in state, so we can compare prior and current values. When values change, the decision is recomputed. I followed the pattern described here, treating the decision state as being derived from the arguments.
The effect that subscribes to updates is also changed to depend on the decision inputs. Since override user attributes are an object, I used a ref combined with a custom equality function to ensure that the dependency array receives a new object reference only when the contents of the attributes are different.
Test Plan:
- Manually tested using an example React app. Checked useFeature and useExperiment return values both before & after client became ready. Checked that useFeature and useExperiment trigger re-renders when entity key or override arguments change.
- Unit tests: updated, and added more
0 commit comments