---
title: Forced Decision methods for the React SDK
slug: feature-experimentation/docs/forced-decision-methods-for-the-react-sdk
docTags: 
createdAt: 2026-08-05T20:39:58.000Z
---

These methods help test and debug various flows of your client applications by forcing users into a specific variation.

The React SDK checks forced decisions before making any decisions. If a matching item is found for the requested flag, the SDK returns the forced decision immediately (audience conditions and traffic allocations are ignored) before making normal decisions.

The React SDK checks for forced decisions at the start of each decision process. If a matching forced decision is found, it returns the decision immediately.

- **Flag-to-Decision** – The SDK checks at the start of any decide call for the given flag.
- **Experiment-Rule-to-Decision** – The SDK checks at the start of the decision for the given experiment rule of the flag key.
- **Delivery-Rule-to-Decision** – The SDK checks at the start of the decision for the given delivery rule of the flag key.

:::hint{type="danger"}
**Warning**
You must associate your variations to a [flag rule](docId:-yq2-thNRv7-SgwCXpG90) before calling any Forced Decision methods.
:::

On forced decisions, the React SDK fires impression events and notifications just like other normal decisions (unless disabled by the disableDecisionEvent option).

:::hint{type="info"}
**Note**
These forced decisions are not persistent and are cleared when the user is updated.
:::

For information about each method, click on the method name.

- [setForcedDecision](docId\:zB-RbK594zBl-vVkyb3Z4)
- [getForcedDecision](docId\:zB-RbK594zBl-vVkyb3Z4)
- [removeForcedDecision](docId\:zB-RbK594zBl-vVkyb3Z4)
- [removeAllForcedDecisions](docId\:zB-RbK594zBl-vVkyb3Z4)

In v4, forced decisions are set on the `userContext` object returned by the `useOptimizelyUserContext` hook, rather than directly on the client instance.

```javascript
import { useOptimizelyUserContext } from '@optimizely/react-sdk';

function MyComponent() {
  const { userContext } = useOptimizelyUserContext();

  const handleSetForced = () => {
    userContext?.setForcedDecision(
      { flagKey: 'product_sort', ruleKey: 'experiment' },
      { variationKey: 'treatment' }
    );
  };

  return <button onClick={handleSetForced}>Force Treatment</button>;
}
```

### OptimizelyDecisionContext

```javascript
export interface OptimizelyDecisionContext {
  flagKey: string;
  ruleKey?: string;
}
```

### OptimizelyForcedDecision

```javascript
export interface OptimizelyForcedDecision {
  variationKey: string;
}
```

## Set forced decision method - setForcedDecision()

### Version

2.8.0

### Description

Sets a forced decision (`variationKey`) for a given `OptimizelyDecisionContext`.

### Parameters

This table lists the required and optional parameters for the React SDK.

| Parameter                        | Type      | Description                                                                                                                                  |
| -------------------------------- | --------- | -------------------------------------------------------------------------------------------------------------------------------------------- |
| **decisionContext** \*required\* | Interface | An instance of \`OptimizelyDecisionContext\` with the required \`flagKey\` and optional \`ruleKey\` for the forced decision you want to set. |
| **decision**\ \*required\*       | Interface | An instance of \`OptimizelyForcedDecision\` with the required \`variationKey\` for the forced decision you want to set.                      |

### Returns

A boolean value that indicates if setting the forced decision (`variationKey`) was completed.

### Example

```javascript
userContext.setForcedDecision(
  { 
    flagKey: "product_sort", 
    ruleKey: "experiment", 
  }, 
  { variationKey: "treatment" }
 );
```

## Get forced decision method - getForcedDecision()

### Version

2.8.0

### Description

Returns the forced decision (`variationKey`) for a given `OptimizelyDecisionContext`. Returns the OptimizelyForcedDecision instance or null if there is no matching item.

### Parameters

This table lists the required and optional parameters for the React SDK.

| Parameter                        | Type      | Description                                                                                                                                  |
| -------------------------------- | --------- | -------------------------------------------------------------------------------------------------------------------------------------------- |
| **decisionContext** \*required\* | Interface | An instance of \`OptimizelyDecisionContext\` with the required \`flagKey\` and optional \`ruleKey\` for the forced decision you want to get. |

### Returns

A forced decision `OptimizelyForcedDecision` instance for the context or null if there is no matching item.

### Example

```javascript
var forcedDecision = userContext.getForcedDecision({
  flagKey: "product_sort",
  ruleKey: "experiment",
});
```

## Remove forced decision method - removeForcedDecision()

### Version

2.8.0

### Description

Removes the forced decision (`variationKey`) for a given `OptimizelyDecisionContext`.

### Parameters

This table lists the required and optional parameters for the React SDK.

| Parameters                       | Type      | Description                                                                                                                                     |
| -------------------------------- | --------- | ----------------------------------------------------------------------------------------------------------------------------------------------- |
| **decisionContext** \*required\* | Interface | An instance of \`OptimizelyDecisionContext\` with the required \`flagKey\` and optional \`ruleKey\` for the forced decision you want to remove. |

### Returns

A success/failure boolean status if the forced decision (`variationKey`) was removed.

### Example

```javascript
var success = userContext.removeForcedDecision({
  flagKey: "product_sort",
  ruleKey: "experiment",
});
```

## Remove all forced decisions method - removeAllForcedDecisions()

### Version

2.8.0

### Description

Removes all forced decisions (`variationKey`) for the user context.

### Parameters

This table lists the required and optional parameters for the React SDK.

| Parameters | Type | Description |
| ---------- | ---- | ----------- |
| None       | N/A  | N/A         |

### Returns

A success/failure boolean status.

### Example

```javascript
var success = userContext.removeAllForcedDecisions();
```

## Full Code Example

```javascript
import React from "react";
import {
  createInstance,
  createPollingProjectConfigManager,
  createBatchEventProcessor,
  OptimizelyProvider,
  useDecide,
  useOptimizelyUserContext,
} from "@optimizely/react-sdk";

const optimizely = createInstance({
  projectConfigManager: createPollingProjectConfigManager({
    sdkKey: "<YOUR_SDK_KEY_HERE>",
  }),
  eventProcessor: createBatchEventProcessor(),
});

function PurchaseButton() {
  const { userContext } = useOptimizelyUserContext();

  const handleClick = () => {
    userContext?.trackEvent("purchased");
  };

  return <div><button onClick={handleClick}>Purchase</button></div>;
}

function ForcedDecisionControls() {
  const { userContext } = useOptimizelyUserContext();

  return (
    <div>
      <button onClick={() => userContext?.setForcedDecision(
        { flagKey: "product_sort" },
        { variationKey: "treatment" }
      )}>
        Set Forced Decision
      </button>
      <button onClick={() => userContext?.removeForcedDecision({ flagKey: "product_sort" })}>
        Remove Forced Decision
      </button>
      <button onClick={() => userContext?.removeAllForcedDecisions()}>
        Remove All Forced Decisions
      </button>
    </div>
  );
}

function ProductSort() {
  const { decision, isLoading, error } = useDecide("product_sort");

  if (isLoading) return <div>Loading...</div>;
  if (error) return <div className="error">{error.message}</div>;

  const variationKey = decision.variationKey;
  const isEnabled = decision.enabled;
  const sortMethod = decision.variables["sort_method"];

  return (
    <div>
      {variationKey === null && <div className="error">{decision.reasons}</div>}
      {isEnabled && <div>The Sort method is {sortMethod}</div>}
      {variationKey === "control" && <div>Control Component</div>}
      {variationKey === "treatment" && <div>Treatment Component</div>}
      <PurchaseButton />
      <ForcedDecisionControls />
    </div>
  );
}

function App() {
  return (
    <OptimizelyProvider
      client={optimizely}
      user={{ id: "user123" }}
    >
      <ProductSort />
    </OptimizelyProvider>
  );
}

export default App;
```
