Skip to content

Junaed/fssdk 12294 sync hook impl#321

Merged
junaed-optimizely merged 11 commits intomasterfrom
junaed/fssdk-12294-sync-hook-impl
Mar 13, 2026
Merged

Junaed/fssdk 12294 sync hook impl#321
junaed-optimizely merged 11 commits intomasterfrom
junaed/fssdk-12294-sync-hook-impl

Conversation

@junaed-optimizely
Copy link
Contributor

@junaed-optimizely junaed-optimizely commented Mar 10, 2026

Summary

New hooks

  • useDecide(flagKey, config?) — Returns a feature flag decision (decision, isLoading, error) for a given flag key. Subscribes to ProviderStateStore via useSyncExternalStore and re-evaluates when client readiness, user context, or forced decisions change.
  • useOptimizelyClient() — Returns the Optimizely client instance from the nearest provider. Does not re-render on store state changes.
  • useOptimizelyUserContext() — Returns the current OptimizelyUserContext (or null). Re-renders only when the user context reference changes, not on unrelated state changes.

Supporting hooks/utilities

  • useOptimizelyContext() — Internal hook that reads React context and throws if used outside .
  • useStableArray(arr) — Preserves referential identity for inline arrays (e.g. decideOptions) when elements are shallowly equal, preventing unnecessary re-evaluations.
  • createDefaultDecision(flagKey) — Helper that produces a safe default OptimizelyDecision while loading or on error.

ProviderStateStore changes

  • Listener notifications are now deferred via qqueueMicrotaskq to avoid the React warning "Cannot update a component while rendering a different component". Multiple synchronous state changes are batched into a single notification.
  • Added subscribeForcedDecision(flagKey, callback) — per-flag-key subscription so useDecide can react to forced decision changes without re-evaluating unrelated flags.

Dependency changes

  • Added use-sync-external-store (with shim for React <18 compat)
  • Removed hoist-non-react-statics

Tests

  • Comprehensive test suites for useDecide, useOptimizelyClient, and useOptimizelyUserContext covering loading states, error handling, reactivity, referential stability, subscription cleanup, and forced decision behavior.
  • Manually tested in different React Envs

Issues

  • FSSDK-12293
  • FSSDK-112295

@junaed-optimizely junaed-optimizely marked this pull request as ready for review March 12, 2026 17:41
Copy link
Contributor

@raju-opti raju-opti left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks great. few small things commented

Copy link
Contributor

@raju-opti raju-opti left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@junaed-optimizely junaed-optimizely merged commit 0659716 into master Mar 13, 2026
9 of 10 checks passed
@junaed-optimizely junaed-optimizely deleted the junaed/fssdk-12294-sync-hook-impl branch March 13, 2026 17:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants