# onRecommendation()

> Calls handler now (once known) and on every change: signing in, a new body, a simulation finishing.

Source: https://docs.tryyoursize.com/reference/android/on-recommendation/

*Method* · `com.tryyoursize:tryyoursize-android`

Calls `handler` now (once known) and on every change: signing in, a new body, a simulation finishing. Keep the subscription; `cancel()` it when done. Main thread.

```kotlin
@JvmOverloads
public fun onRecommendation(
        product: String,
        onError: ((TryYourSizeException) -> Unit)? = null,
        handler: (Recommendation) -> Unit,
    ): TysSubscription
```

## Parameters

- **`product`** (`String`, required): Your product ID or SKU.
- **`onError`** (`((TryYourSizeException) -> Unit)?`, optional, default `null`): Called when our window cannot be reached or the app is not registered.
- **`handler`** (`(Recommendation) -> Unit`, required): Called now (once known) and on every change.

## Returns

A subscription; `cancel()` it when done.

## Example

**Kotlin**

```kotlin
val sub = tys.onRecommendation("SKU-123") { rec ->
    if (rec.status == Recommendation.Status.READY && !touched) selected = rec.size
}
// sub.cancel() in onDestroy
```

**Swift**

```swift
let sub = tys.onRecommendation(for: "SKU-123") { rec in
    if rec.status == .ready, !touched { selectedSize = rec.size }
}
// keep `sub`; sub.cancel() when the screen goes away
```

**React Native**

```tsx
useEffect(() => {
  const sub = addRecommendationListener("SKU-123", (rec) => {
    if (rec.status === "ready" && !touched) setSize(rec.size);
  });
  return () => sub.remove();
}, []);
```

**Flutter**

```dart
final sub = tys.onRecommendation('SKU-123').listen((rec) {
  if (rec.status == RecommendationStatus.ready && !touched) setState(() => size = rec.size);
});
// sub.cancel() in dispose()
```
