# ty-react-native-netinfo **Repository Path**: harmonytsc_rn_ty/ty-react-native-netinfo ## Basic Information - **Project Name**: ty-react-native-netinfo - **Description**: ty-react-native-netinfo库 - **Primary Language**: Unknown - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-03-31 - **Last Updated**: 2026-02-02 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README > 模板版本:v0.2.2

@tuya-oh/netinfo

Supported platforms License

> [!TIP] [Gitee 地址](https://gitee.com/kunyuan-hongke/ty-react-native-netinfo) ## 安装与使用 请到三方库的 Releases 发布地址查看配套的版本信息:[@tuya-oh/netinfo Releases](https://gitee.com/kunyuan-hongke/ty-react-native-netinfo/releases) 。对于未发布到npm的旧版本,请参考[安装指南](https://gitee.com/react-native-oh-library/usage-docs/blob/master/zh-cn/tgz-usage.md)安装tgz包。 进入到工程目录并输入以下命令: #### **npm** ```bash npm install @tuya-oh/netinfo ``` #### **yarn** ```bash yarn add @tuya-oh/netinfo ``` 下面的代码展示了这个库的基本使用场景: > [!WARNING] 使用时 import 的库名不变。 ```js import React, { Component } from "react"; import { View, Text, Button, SafeAreaView } from "react-native"; import NetInfo from "@react-native-community/netinfo"; export class NetInfoDemo extends Component { constructor(props) { super(props); this.state = { connectionInfo: null, isConnected: false, effectiveType: "unknown", type: "unknown", }; } componentDidMount() { // 初始化时获取连接信息 this.fetchConnectionInfo(); // 监听连接状态变化 this.unsubscribe = NetInfo.removeEventListener( "connectionChange", this.handleConnectionChange ); } componentWillUnmount() { // 移除监听 this.unsubscribe(); } // 获取完整连接信息 fetchConnectionInfo = async () => { try { const info = await NetInfo.getConnectionInfo(); this.setState({ connectionInfo: JSON.stringify(info, null, 2), effectiveType: info?.effectiveType, type: info?.type, }); } catch (error) { console.error("获取连接信息失败:", error); } }; // 检查简单连接状态 checkConnection = async () => { try { const isConnected = await NetInfo.isConnected.fetch(); this.setState({ isConnected }); } catch (error) { console.error("检查连接状态失败:", error); } }; // 处理连接变化 handleConnectionChange = async (isConnected) => { this.setState({ isConnected }); await this.fetchConnectionInfo(); // 状态变化时重新获取详细信息 }; render() { return ( 网络状态检测