Skip to content

Commit 3ddc446

Browse files
committed
docs: clarify useIAP hook state-driven design pattern in llms.txt
- Add Design Pattern section explaining void-returning methods - Clearly separate STATE VARIABLES from METHODS - Add Usage Pattern examples showing correct vs incorrect usage - Note difference between hook getAvailablePurchases (void) and root API (Purchase[]) - Mark exceptions that return values (hasActiveSubscriptions, verifyPurchase, etc.)
1 parent d4f8bdc commit 3ddc446

File tree

2 files changed

+71
-15
lines changed

2 files changed

+71
-15
lines changed

docs/static/llms-full.txt

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,22 +91,29 @@ interface UseIAPOptions {
9191

9292
```tsx
9393
interface UseIAPReturn {
94-
// State
94+
// ═══════════════════════════════════════════════════════════════
95+
// STATE VARIABLES (read data from these)
96+
// ═══════════════════════════════════════════════════════════════
9597
connected: boolean;
9698
products: Product[];
9799
subscriptions: ProductSubscription[];
98100
availablePurchases: Purchase[];
99101
activeSubscriptions: ActiveSubscription[];
100102
promotedProductIOS?: Product;
101103

102-
// Methods (return void, update state)
104+
// ═══════════════════════════════════════════════════════════════
105+
// METHODS (return Promise<void>, update state above)
106+
// ═══════════════════════════════════════════════════════════════
103107
fetchProducts: (params: {skus: string[]; type: 'in-app' | 'subs'}) => Promise<void>;
104108
requestPurchase: (props: RequestPurchaseProps) => Promise<void>;
105109
finishTransaction: (params: {purchase: Purchase; isConsumable?: boolean}) => Promise<void>;
106110
getAvailablePurchases: () => Promise<void>;
111+
getActiveSubscriptions: (ids?: string[]) => Promise<void>;
112+
restorePurchases: () => Promise<void>;
107113

108-
// Methods (return values)
109-
getActiveSubscriptions: (ids?: string[]) => Promise<ActiveSubscription[]>;
114+
// ═══════════════════════════════════════════════════════════════
115+
// METHODS THAT RETURN VALUES (exceptions to the pattern)
116+
// ═══════════════════════════════════════════════════════════════
110117
hasActiveSubscriptions: (ids?: string[]) => Promise<boolean>;
111118
verifyPurchase: (props: VerifyPurchaseProps) => Promise<VerifyPurchaseResult>;
112119
verifyPurchaseWithProvider: (props: VerifyPurchaseWithProviderProps) => Promise<VerifyPurchaseWithProviderResult>;
@@ -117,10 +124,29 @@ interface UseIAPReturn {
117124
}
118125
```
119126

127+
### Design Pattern
128+
129+
**The hook follows React's state-driven pattern:**
130+
- Methods return `Promise<void>` and update internal state
131+
- You must read data from state variables, not from method return values
132+
133+
```tsx
134+
// ✅ CORRECT: Call method, then read from state
135+
await fetchProducts({skus: ['product1'], type: 'in-app'});
136+
console.log(products); // Read from state variable
137+
138+
await getAvailablePurchases();
139+
console.log(availablePurchases); // Read from state variable
140+
141+
// ❌ WRONG: Don't expect return values from methods
142+
const result = await fetchProducts({...}); // result is void!
143+
const purchases = await getAvailablePurchases(); // purchases is void!
144+
```
145+
120146
### Important Behavior
121147

122148
- **Auto-connects**: Calls `initConnection()` on mount, `endConnection()` on unmount
123-
- **Void-returning methods**: `fetchProducts`, `requestPurchase`, `getAvailablePurchases` return `Promise<void>` and update internal state
149+
- **Void-returning methods**: `fetchProducts`, `requestPurchase`, `getAvailablePurchases`, `getActiveSubscriptions` return `Promise<void>` and update internal state
124150
- **Use callbacks**: Always handle purchases via `onPurchaseSuccess` and `onPurchaseError`
125151

126152
### Basic Example

docs/static/llms.txt

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -57,31 +57,59 @@ function Store() {
5757

5858
The main interface for in-app purchases. Auto-manages connection lifecycle.
5959

60+
**Design Pattern:** The hook follows React's state-driven pattern. Methods return `Promise<void>` and update internal state. You must read data from the returned state variables, not from method return values.
61+
6062
```tsx
6163
const {
64+
// ═══════════════════════════════════════════════════════════════
65+
// STATE VARIABLES (read data from these)
66+
// ═══════════════════════════════════════════════════════════════
6267
connected, // boolean - IAP service ready
63-
products, // Product[] - fetched products
64-
subscriptions, // ProductSubscription[] - fetched subscriptions
65-
availablePurchases, // Purchase[] - restorable purchases
66-
activeSubscriptions, // ActiveSubscription[] - active subscriptions
67-
fetchProducts, // ({skus, type}) => Promise<void>
68-
requestPurchase, // (props) => Promise<void>
69-
finishTransaction, // ({purchase, isConsumable}) => Promise<void>
68+
products, // Product[] - populated by fetchProducts()
69+
subscriptions, // ProductSubscription[] - populated by fetchProducts()
70+
availablePurchases, // Purchase[] - populated by getAvailablePurchases()
71+
activeSubscriptions, // ActiveSubscription[] - populated by getActiveSubscriptions()
72+
73+
// ═══════════════════════════════════════════════════════════════
74+
// METHODS (return Promise<void>, update state above)
75+
// ═══════════════════════════════════════════════════════════════
76+
fetchProducts, // ({skus, type}) => Promise<void>
77+
requestPurchase, // (props) => Promise<void>
78+
finishTransaction, // ({purchase, isConsumable}) => Promise<void>
7079
getAvailablePurchases, // () => Promise<void>
71-
getActiveSubscriptions, // (ids?) => Promise<ActiveSubscription[]>
80+
getActiveSubscriptions, // (ids?) => Promise<void>
81+
restorePurchases, // () => Promise<void>
82+
83+
// ═══════════════════════════════════════════════════════════════
84+
// METHODS THAT RETURN VALUES (exceptions to the pattern)
85+
// ═══════════════════════════════════════════════════════════════
7286
hasActiveSubscriptions, // (ids?) => Promise<boolean>
7387
verifyPurchase, // (props) => Promise<VerifyPurchaseResult>
7488
verifyPurchaseWithProvider, // (props) => Promise<result>
7589
} = useIAP(options);
7690
```
7791

92+
**Usage Pattern:**
93+
```tsx
94+
// ✅ CORRECT: Call method, then read from state
95+
await fetchProducts({skus: ['product1'], type: 'in-app'});
96+
console.log(products); // Read from state variable
97+
98+
await getAvailablePurchases();
99+
console.log(availablePurchases); // Read from state variable
100+
101+
// ❌ WRONG: Don't expect return values from methods
102+
const result = await fetchProducts({...}); // result is void!
103+
const purchases = await getAvailablePurchases(); // purchases is void!
104+
```
105+
78106
**Options:**
79107
- `onPurchaseSuccess?: (purchase: Purchase) => void` - Success callback
80108
- `onPurchaseError?: (error: PurchaseError) => void` - Error callback
81109

82110
### Direct API Functions
83111

84-
For use outside React components:
112+
For use outside React components. Unlike hook methods, these return data directly.
85113

86114
```tsx
87115
import {
@@ -90,14 +118,16 @@ import {
90118
fetchProducts, // ({skus, type}) => Promise<Product[]>
91119
requestPurchase, // (props) => Promise<Purchase | Purchase[] | void>
92120
finishTransaction, // ({purchase, isConsumable}) => Promise<void>
93-
getAvailablePurchases, // () => Promise<Purchase[]>
121+
getAvailablePurchases, // (options?) => Promise<Purchase[]> - returns Purchase array directly
94122
getActiveSubscriptions, // (ids?) => Promise<ActiveSubscription[]>
95123
hasActiveSubscriptions, // (ids?) => Promise<boolean>
96124
deepLinkToSubscriptions, // ({skuAndroid?}) => Promise<void>
97125
getStorefront, // () => Promise<string>
98126
} from 'expo-iap';
99127
```
100128

129+
**Note:** The root API `getAvailablePurchases` returns `Promise<Purchase[]>`, while the hook's `getAvailablePurchases` returns `Promise<void>` and updates internal state.
130+
101131
### Event Listeners
102132

103133
```tsx

0 commit comments

Comments
 (0)