This repository was archived by the owner on Apr 26, 2026. It is now read-only.
Commit 1bbbbae
perf(hooks): extract restorePurchases into useCallback for stable reference (#3172)
## Summary
Extracts the inline `restorePurchases` function from the `useIAP` hook's
return statement into a properly memoized `useCallback`.
## Problem
All other public functions returned by `useIAP` (e.g.
`finishTransaction`, `requestPurchase`, `fetchProducts`,
`validateReceipt`) are wrapped in `useCallback` to provide a stable
reference across renders. However, `restorePurchases` was defined as an
inline `async () => {}` directly in the return statement, causing it to
be recreated on every render.
This inconsistency means:
- Components that receive `restorePurchases` as a prop and use it in a
`useEffect` or `useCallback` dependency array will trigger unnecessary
re-runs
- Strict mode linting (e.g. `react-hooks/exhaustive-deps`) can flag its
usage since it's not stable
- It's inconsistent with the rest of the hook's API surface
## Changes
- Extracted `restorePurchases` into a `useCallback` with appropriate
dependencies `[getAvailablePurchasesInternal, invokeOnError]`
- Removed the now-outdated comment `// No local restorePurchases; use
the top-level helper via returned API`
- The return statement now references the stable `restorePurchases`
variable
## Why
Improves performance consistency and aligns `restorePurchases` with the
rest of the hook's API design.
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit
* **Refactor**
* Updated restore flow: on iOS the app now performs a platform sync
before refreshing purchases. No change to public API or expected
behavior for end users.
* **Tests**
* Test suite updated to reflect the new iOS restore sequence and error
propagation scenarios.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
---------
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>1 parent 4384366 commit 1bbbbae
2 files changed
Lines changed: 63 additions & 16 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
44 | 44 | | |
45 | 45 | | |
46 | 46 | | |
47 | | - | |
| 47 | + | |
48 | 48 | | |
49 | 49 | | |
50 | 50 | | |
| |||
61 | 61 | | |
62 | 62 | | |
63 | 63 | | |
64 | | - | |
65 | | - | |
| 64 | + | |
| 65 | + | |
66 | 66 | | |
67 | 67 | | |
68 | 68 | | |
| |||
240 | 240 | | |
241 | 241 | | |
242 | 242 | | |
243 | | - | |
| 243 | + | |
244 | 244 | | |
245 | | - | |
| 245 | + | |
246 | 246 | | |
247 | 247 | | |
248 | 248 | | |
| |||
260 | 260 | | |
261 | 261 | | |
262 | 262 | | |
| 263 | + | |
263 | 264 | | |
264 | 265 | | |
265 | 266 | | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
266 | 310 | | |
267 | 311 | | |
268 | 312 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
18 | 18 | | |
19 | 19 | | |
20 | 20 | | |
21 | | - | |
| 21 | + | |
22 | 22 | | |
23 | 23 | | |
24 | 24 | | |
| |||
333 | 333 | | |
334 | 334 | | |
335 | 335 | | |
336 | | - | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
337 | 348 | | |
338 | 349 | | |
339 | 350 | | |
| |||
506 | 517 | | |
507 | 518 | | |
508 | 519 | | |
509 | | - | |
510 | | - | |
511 | | - | |
512 | | - | |
513 | | - | |
514 | | - | |
515 | | - | |
516 | | - | |
517 | | - | |
| 520 | + | |
518 | 521 | | |
519 | 522 | | |
520 | 523 | | |
| |||
0 commit comments