> This is a page from the ElevenLabs documentation. For a complete page index, fetch https://elevenlabs.io/docs/llms.txt. For the full documentation in a single file, fetch https://elevenlabs.io/docs/llms-full.txt.

# React Native SDK

> **Info**
>
> Zobacz [omówienie ElevenAgents](/docs/pl/eleven-agents/overview), aby dowiedzieć się, jak działa
> ElevenAgents.

## Instalacja

Zainstaluj pakiet i jego zależności w projekcie React Native.

```shell
npm install @elevenlabs/react-native @livekit/react-native @livekit/react-native-webrtc livekit-client
```

> **Tip**
>
> Przykładową aplikację korzystającą z tego SDK z Expo znajdziesz
> [tutaj](https://github.com/elevenlabs/packages/tree/main/examples/react-native-expo)

> **Tip**
>
> Aktualizujesz starszą wersję? Uruchom `npx skills add elevenlabs/packages`, aby zainstalować
> umiejętność `elevenlabs:sdk-migration` dla swojego agenta AI do programowania. Automatyzuje ona zmiany importów,
> opakowanie w `ConversationProvider` i aktualizacje API.

## Wymagania

* React Native z zależnościami LiveKit
* Uprawnienia do mikrofonu skonfigurowane dla twojej platformy
* Zgodność z Expo (tylko buildy deweloperskie)
* React Native 0.79.0+ albo Metro skonfigurowane do rozpoznawania `"exports"` w `package.json` w starszych wersjach

> **Warning**
>
> To SDK zaprojektowano i stworzono do użycia z frameworkiem Expo. Ze względu na zależność od
> implementacji WebRTC LiveKit wymaga buildów deweloperskich i nie działa z Expo Go.

> **Warning**
>
> `@elevenlabs/react-native` rozpoznaje punkt wejścia React Native przez pole
> [`"exports"`](https://nodejs.org/api/packages.html#exports) w `package.json`. Metro domyślnie obsługuje
> `"exports"` dopiero od wersji Metro 0.82.0, wydanej w [React Native 0.79.0](https://reactnative.dev/blog/2025/04/08/react-native-0.79) (8 kwietnia 2025 r.). Jeśli używasz
> React Native starszego niż 0.79, ustaw `resolver.unstable_enablePackageExports =
>   true` w `metro.config.js`, aby ten pakiet był poprawnie rozpoznawany:
>
> ```js
> const { getDefaultConfig } = require("metro-config");
>
> const config = getDefaultConfig(__dirname);
> config.resolver.unstable_enablePackageExports = true;
>
> module.exports = config;
> ```

## Konfiguracja

### Konfiguracja providera

Otocz aplikację komponentem `ConversationProvider`, aby włączyć funkcje ElevenAgents.

```tsx
import { ConversationProvider } from "@elevenlabs/react-native";
import React from "react";

function App() {
  return (
    <ConversationProvider>
      <YourAppComponents />
    </ConversationProvider>
  );
}
```

> **Note**
>
> `@elevenlabs/react-native` ponownie eksportuje `ConversationProvider` i wszystkie hooki z
> `@elevenlabs/react`. API jest takie samo jak w webowym SDK React — pełną dokumentację API znajdziesz w [dokumentacji SDK React](/docs/pl/eleven-agents/libraries/react).

## Użycie

Wszystkie hooki z SDK React są dostępne przez `@elevenlabs/react-native`:

* **`useConversation`** — wygodny hook łączący cały stan i metody
* **`useConversationControls`** — metody działania (startSession, endSession itp.)
* **`useConversationStatus`** — status połączenia
* **`useConversationInput`** — stan wyciszenia
* **`useConversationMode`** — stan mówienia/słuchania
* **`useConversationFeedback`** — dostępność i wysyłanie opinii
* **`useConversationClientTool`** — dynamiczna rejestracja narzędzi klienta
* **`useRawConversation`** — surowa instancja rozmowy

### Rozpoczynanie rozmowy

```tsx
import { useConversationControls, useConversationStatus } from "@elevenlabs/react-native";
import React from "react";
import { View, Text, Button } from "react-native";

function ConversationComponent() {
  const { startSession, endSession } = useConversationControls();
  const { status } = useConversationStatus();

  const handleStart = async () => {
    await startSession({
      agentId: "agent_7101k5zvyjhmfg983brhmhkd98n6",
    });
  };

  return (
    <View>
      <Text>Status: {status}</Text>
      <Button
        title={status === "connected" ? "End" : "Start"}
        onPress={status === "connected" ? endSession : handleStart}
      />
    </View>
  );
}
```

Informacje o prywatnych agentach, uwierzytelnianiu, narzędziach klienta, nadpisaniach i wszystkich pozostałych funkcjach znajdziesz w [dokumentacji SDK React](/docs/pl/eleven-agents/libraries/react).

## Przykładowa implementacja

Oto kompletny przykład komponentu React Native korzystającego z SDK ElevenLabs Agents:

```tsx
import { ConversationProvider, useConversation } from "@elevenlabs/react-native";
import React, { useState } from "react";
import { View, Text, TouchableOpacity, StyleSheet } from "react-native";

function ConversationScreen() {
  const [isConnected, setIsConnected] = useState(false);

  const conversation = useConversation({
    onConnect: () => {
      console.log("Connected to conversation");
      setIsConnected(true);
    },
    onDisconnect: () => {
      console.log("Disconnected from conversation");
      setIsConnected(false);
    },
    onMessage: (message) => {
      console.log("Message received:", message);
    },
    onError: (error) => {
      console.error("Conversation error:", error);
    },
  });

  const startConversation = async () => {
    try {
      await conversation.startSession({
        agentId: "agent_7101k5zvyjhmfg983brhmhkd98n6",
      });
    } catch (error) {
      console.error("Failed to start conversation:", error);
    }
  };

  const endConversation = async () => {
    try {
      await conversation.endSession();
    } catch (error) {
      console.error("Failed to end conversation:", error);
    }
  };

  return (
    <View style={styles.container}>
      <Text style={styles.status}>Status: {conversation.status}</Text>

      <Text style={styles.speaking}>
        Agent is {conversation.isSpeaking ? "speaking" : "not speaking"}
      </Text>

      <TouchableOpacity
        style={[styles.button, isConnected && styles.buttonActive]}
        onPress={isConnected ? endConversation : startConversation}
      >
        <Text style={styles.buttonText}>
          {isConnected ? "End Conversation" : "Start Conversation"}
        </Text>
      </TouchableOpacity>

      {conversation.canSendFeedback && (
        <View style={styles.feedbackContainer}>
          <TouchableOpacity
            style={styles.feedbackButton}
            onPress={() => conversation.sendFeedback(true)}
          >
            <Text>👍</Text>
          </TouchableOpacity>
          <TouchableOpacity
            style={styles.feedbackButton}
            onPress={() => conversation.sendFeedback(false)}
          >
            <Text>👎</Text>
          </TouchableOpacity>
        </View>
      )}
    </View>
  );
}

function App() {
  return (
    <ConversationProvider>
      <ConversationScreen />
    </ConversationProvider>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: "center",
    alignItems: "center",
    padding: 20,
  },
  status: {
    fontSize: 16,
    marginBottom: 10,
  },
  speaking: {
    fontSize: 14,
    marginBottom: 20,
    color: "#666",
  },
  button: {
    backgroundColor: "#007AFF",
    paddingHorizontal: 20,
    paddingVertical: 10,
    borderRadius: 8,
    marginBottom: 20,
  },
  buttonActive: {
    backgroundColor: "#FF3B30",
  },
  buttonText: {
    color: "white",
    fontSize: 16,
    fontWeight: "600",
  },
  feedbackContainer: {
    flexDirection: "row",
    gap: 10,
  },
  feedbackButton: {
    backgroundColor: "#F2F2F7",
    padding: 10,
    borderRadius: 8,
  },
});

export default App;
```

## Informacje dla poszczególnych platform

### iOS

Upewnij się, że uprawnienia do mikrofonu są poprawnie skonfigurowane w `Info.plist`:

```xml
<key>NSMicrophoneUsageDescription</key>
<string>This app needs microphone access to enable voice conversations with AI agents.</string>
```

### Android

Dodaj uprawnienia do mikrofonu w `android/app/src/main/AndroidManifest.xml`:

```xml
<uses-permission android:name="android.permission.RECORD_AUDIO" />
```

Rozważ poproszenie o uprawnienia w czasie działania aplikacji przed rozpoczęciem rozmowy:

```tsx
import { PermissionsAndroid, Platform } from "react-native";

const requestMicrophonePermission = async () => {
  if (Platform.OS === "android") {
    const granted = await PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.RECORD_AUDIO, {
      title: "Microphone Permission",
      message: "This app needs microphone access to enable voice conversations.",
      buttonNeutral: "Ask Me Later",
      buttonNegative: "Cancel",
      buttonPositive: "OK",
    });
    return granted === PermissionsAndroid.RESULTS.GRANTED;
  }
  return true;
};
```