# 组件和钩子 (/zh-CN/api-reference/rtm/react-native/components-hooks) > For AI agents: see the complete documentation index at [llms.txt](/llms.txt). ## 组件 [#组件] ### RTMProvider [#rtmprovider] 该组件向子组件[提供 Context](https://react.dev/learn/passing-data-deeply-with-context),让 `children` 内的各个组件都能读取你传入的 `client` 属性。 #### Props [#props] | 属性 | 类型 | 默认值 | 描述 | | --- | --- | --- | --- | | `client` | `RTMClient` | 是 | 通过 React Native SDK 的 [`createAgoraRtmClient`](/zh-CN/api-reference/rtm/react-native/configuration#createagorartmclient) 创建的 `RTMClient` 对象。| | `children` | `ReactNativeNode` | 无 | 需要展示的 ReactNative 节点。| #### 基本用法 [#基本用法] ```jsx function App({ children }) { const [client] = useState(() => createAgoraRtmClient( new RtmConfig({ userId: uid, appId: appId, }) )); return {children}; } ``` ## 钩子 [#钩子] ### useLogin [#uselogin] 用于登录 RTM 服务。当组件准备好时登录 RTM 服务,当组件卸载时自动登出 RTM 服务。 #### 参数 [#参数] | 参数名 | 类型 | 是否必填 | 描述 | | --- | --- | --- | --- | | `client` | `RTMClient` | 是 | 通过 React Native SDK 的 [`createAgoraRtmClient`](/zh-CN/api-reference/rtm/react-native/configuration#createagorartmclient) 创建的 `RTMClient` 对象。| | `loginOptions` | `LoginOptions` | 是 | 登录选项。详见 [`login`](/zh-CN/api-reference/rtm/react-native/configuration#login)。| #### 基本用法 [#基本用法-1] ```jsx useLogin(client, { token: "your_token" }); ``` ### useRtm [#usertm] 用于获取 `RTMClient` 对象。 #### 参数 [#参数-1] | 参数名 | 类型 | 是否必填 | 描述 | | --- | --- | --- | --- | | `client` | `RTMClient` \| `null` | 否 | 通过 React Native SDK 的 [`createAgoraRtmClient`](/zh-CN/api-reference/rtm/react-native/configuration#createagorartmclient) 创建的 `RTMClient` 对象。| #### 返回值 [#返回值] 返回一个 `RTMClient` 对象。 #### 基本用法 [#基本用法-2] ```jsx const rtmClient = useRtm(); ``` ### useRtmEvent [#usertmevent] 用于监听 `RTMClient` 对象的指定事件。 - 当组件挂载时,该 Hook 会注册对应的事件监听器。 - 当组件卸载时,该 Hook 会销毁对应的事件监听器。 #### 参数 [#参数-2] | 参数名 | 类型 | 是否必填 | 描述 | | --- | --- | --- | --- | | `client` | `RTMClient` | 是 | 通过 React Native SDK 的 [`createAgoraRtmClient`](/zh-CN/api-reference/rtm/react-native/configuration#createagorartmclient) 创建的 `RTMClient` 对象。| | `event` | string | 是 | 事件名称。支持的事件详见[添加监听](/zh-CN/api-reference/rtm/react-native/configuration#添加监听)。| | `listener` | Function | 是 | 回调函数。当传入的事件触发时,该函数会被调用。 | #### 基本用法 [#基本用法-3] ```jsx useRtmEvent(client, 'message', (message: MessageEvent) => { // your code }); ```