---
title: Forced Decision methods for the React Native SDK
slug: feature-experimentation/docs/forced-decision-methods-reactnative-prior-to-v4
docTags: 
createdAt: 2026-08-05T20:40:01.000Z
---

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

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

The following describes specific scenarios the React Native SDK will follow:

**Flag-to-Decision**

- SDK will look up at the beginning of any **decide** call for the given flag. If a matching Flag-to-Decision forced decision is found for the flag, it returns the decision.

**Experiment-Rule-to-Decision**

- SDK will look up at the beginning of the decision for the given experiment rule (of the flag key). If a matching Experiment-Rule-to-Decision forced decision is found for the flag, it returns the decision.

**Delivery-Rule-to-Decision**

- SDK will look up at the beginning of the decision for the given delivery rule (of the flag key). If a matching Delivery-Rule-to-Decision forced decision is found, it returns the decision.

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

On forced decisions, the React Native SDK will fire 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 will be cleared when the user is updated.
:::

For information about each method select a method name from the following list:

- [setForcedDecision](docId:6dcrqPGqP90A3CNgtziMC)
- [getForcedDecision](docId:6dcrqPGqP90A3CNgtziMC)
- [removeForcedDecision](docId:6dcrqPGqP90A3CNgtziMC)
- [removeAllForcedDecisions](docId:6dcrqPGqP90A3CNgtziMC)

If you are calling forced decision methods right after initialization, ensure you wait for the user to be ready using the `onUserUpdate` promise.

```javascript
const optimizely = createInstance({ 
 sdkKey: '<Your_SDK_Key>', 
}); 

optimizely.onUserUpdate(() => {
  optimizely.setForcedDecision( 
    { 
      flagKey: "product_sort", 
      ruleKey: "experiment", 
    }, 
    { variationKey: "treatment" } 
  );
});
```

### 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 Native 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
optimizely.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 Native 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 = optimizely.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 Native 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 = optimizely.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 Native SDK.

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

### Returns

A success/failure boolean status.

### Example

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

## Full Code Example

```javascript
import React from "react"; 
import { 
  createInstance, 
  OptimizelyProvider, 
  useDecision, 
  withOptimizely, 
} from "@optimizely/react-sdk"; 

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

optimizely.onUserUpdate(() => { 
  // Wait for the user to be available before using forced decision methods. 
  optimizely.setForcedDecision({ flagKey: "product_sort" }, { variationKey: "treatment" });
}); 

const PurchaseButton = withOptimizely((props: any) => { 
  const handleClick = () => { 
    const { optimizely } = props; 
    optimizely.track("purchased"); 
  }; 
  return <div><button onClick={handleClick}>Purchase</button></div>; 
}); 

const RemoveForcedDecision = withOptimizely((props: any) => { 
  const handleClick = () => {
    const { optimizely } = props; 
    optimizely.removeForcedDecision({ flagKey: 'product_sort' });
  };
  return <div><button onClick={handleClick}>Remove Forced Decision</button></div>; 
});

const RemoveAllForcedDecisions = withOptimizely((props: any) => { 
  const handleClick = () => { 
    const { optimizely } = props; 
    optimizely.removeAllForcedDecisions(); 
  }; 
  return <div><button onClick={handleClick}>Remove All Forced Decisions</button></div>; 
}); 

function ProductSort() { 
  const [decision] = useDecision("product_sort", { 
    autoUpdate: true 
  }); 

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

  return ( 
     <div> 
       {/* If variation is null, display error */} 
       {variationKey === null && <div className="error">{decision.reasons}</div>} 

       {/* If feature is enabled, do something with the Sort Method variable */} 
       {isEnabled && <div>The Sort method is {sortMethod}</div>} 

       {/* Show relevant component based on the variation key */} 
       {variationKey === "control" && <div>Control Component</div>} 
       {variationKey === "treatment" && <div>Treatment Component</div>} 

       {/* Show Purchase Button to track Purchase event */} 
       <PurchaseButton /> 
       <RemoveForcedDecision /> 
       <RemoveAllForcedDecisions /> 
     </div> 
  ); 
}

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

export default App;
```
