Movatterモバイル変換


[0]ホーム

URL:


Dev guideRecipesAPI ReferenceChangelog
Dev guideAPI ReferenceRecipesChangelogUser GuideGitHubDev CommunityOptimizely AcademySubmit a ticketLog InFeature Experimentation
Dev guide
All
Pages
Start typing to search…

useDecision hook for the React SDK

The useDecision hook retrieves the decision result for a flag key for the Optimizely Feature Experimentation React SDK.

Minimum SDK version – v2.5+

useDecision hook

Retrieves the decision result for a flag key, optionally auto-updating that decision based on underlying user, datafile, or forced decision changes.

Arguments

Argument

Type

Description

flagKey(required)

String

The key of the feature flag

options(optional)

Object

Includes the following:

  • autoUpdate (boolean) - If true, this hook updates the flag decision in response to datafile or user changes. Default:false.
  • timeout (number) - Client timeout as described in theOptimizelyProvider documentation. Overrides any timeout set on the ancestorOptimizelyProvider.
  • decideOptions (OptimizelyDecideOption) - Array ofOptimizelyDecideOption enums. SeeOptimizelyDecideOption.

overrides(optional)

Object

Includes the following:

  • overrideUserId (string) - Override the userId to be used to obtain the decision result for this hook.
  • overrideAttributes (optimizely.UserAttributes) – Override the user attributes to be used to obtain the decision result for this hook.

Returns

Returns an array with the following:

Key

Type

Description

decision

OptimizelyDecision

Decision result for the flag key.

clientReady

Boolean

Indicates whether theReactSDKClient instance is ready.

didTimeout

Boolean

Indicates whether theReactSDKClient instance became ready within the allowed timeout range.

clientReady can betrue even ifdidTimeout is alsotrue. This indicates that the client became ready after the timeout period.

ExampleuseDecision

import { useEffect } from 'react';import { useDecision } from '@optimizely/react-sdk';function LoginComponent() {  const [decision, clientReady] = useDecision(    'flag1',    { autoUpdate: true },    {      /* (Optional) User overrides */    }  );  useEffect(() => {    document.title = decision.enabled ? 'New Feature flag' : 'Old Feature flag';  }, [decision]);  return (    <p>      <a href={decision.variationKey === 'login1' ? '/login' : '/login2'}>Click to login</a>    </p>  );}

OptimizelyDecideOption

The following table lists theOptimizelyDecideOption enum with an explanation what happens if you set them. In addition to setting these options individually for a decide method, you can also set them as global defaults when you instantiate the Optimizely client. SeeInitialize the React SDK.

OptimizelyDecideOption

enum

If set

OptimizelyDecideOption.DISABLE_DECISION_EVENT

Prevents the SDK from dispatching animpression event when serving a variation. This disables decision tracking on theOptimizely Experiment Results page and thedecision notification listener.

OptimizelyDecideOption.ENABLED_FLAGS_ONLY

Returns decisions only for flags that are currently enabled. Used with thedecide all method anddecide for keys method.

When this option is not set, the Android SDK returns all decisions regardless of whether the flag is enabled.

OptimizelyDecideOption.IGNORE_USER_PROFILE_SERVICE

Bypasses the user profile service (both lookup and save) for the decision.

When this option is not set, user profile service overrides audience targeting, traffic allocation, and experiment mutual exclusion groups.

OptimizelyDecideOption.INCLUDE_REASONS

Adds log messages to the reasons field of the decision. Critical errors are always returned, even if this option is not set.

OptimizelyDecideOption.EXCLUDE_VARIABLES

Excludes flag variable values from the decision result. Use this option to minimize the returned decision by skipping large JSON variables.

The following example shows how you can set options individually on theuseDecision hook, or as global defaults when you instantiate the Optimizely client. SeeInitialize React SDK.

import { useEffect } from 'react';import {  createInstance,  OptimizelyProvider,  useDecision,  OptimizelyDecideOption,} from '@optimizely/react-sdk';// Instantiate an Optimizely clientconst optimizelyClient = createInstance({  sdkKey: 'Your_SDK_Key', // Replace with your SDK key  defaultDecideOptions: [OptimizelyDecideOption.DISABLE_DECISION_EVENT],});function LoginComponent() {  const [decision, clientReady] = useDecision(    'flag1',    {      autoUpdate: true,      decideOptions: [        OptimizelyDecideOption.ENABLED_FLAGS_ONLY,        OptimizelyDecideOption.IGNORE_USER_PROFILE_SERVICE,      ],    },    {      /* (Optional) User overrides */    }  );  useEffect(() => {    document.title = decision.enabled ? 'New Feature flag' : 'Old Feature flag';  }, [decision]);  return (    <p>      <a href={decision.variationKey === 'login1' ? '/login' : '/login2'}>Click to login</a>    </p>  );}function App() {  return (    <OptimizelyProvider optimizely={optimizelyClient} user={{ id: 'user123' }}>      <LoginComponent />    </OptimizelyProvider>  );}

Updated 19 days ago



[8]ページ先頭

©2009-2025 Movatter.jp