一款用于react-native监听app[AppState]前后台的自定义Hooks开源插件

react-native-watch-appstate

React Native appState hook is a custom react hook, built to handle iOS or Android or Harmony appState in your react component

自定义 appState hooks 适用于三端[android/ios/harmony]

_ AppState 可以告诉您应用程序是在前台还是后台,并在状态改变时通知您。 _

rn 官方地址 appState https://reactnative.dev/docs/appstate

安装

yarn add react-native-watch-appstate

OR

npm install react-native-watch-appstate


Example

javascript 复制代码
import React from "react";
import { Text, View } from "react-native";
import useAppState from "react-native-appstate-hook";

export default function App() {
  const appState = useAppState({
    onChange: (newAppState) =>
      console.warn("App state changed to ", newAppState),
    onForeground: () => console.warn("App went to Foreground"),
    onBackground: () => console.warn("App went to background"),
  });

  return (
    <View
      style={{
        textAlign: "center",
        backgroundColor: "white",
        flex: 1,
        justifyContent: "center",
      }}
    >
      <Text>App State is: {appState}</Text>
    </View>
  );
}
相关推荐
鼎道开发者联盟1 小时前
2025中国AI开源生态报告发布,鼎道智联助力产业高质量发展
人工智能·开源·gui
大厂技术总监下海1 小时前
Rust的“一发逆转弹”:Dioxus 如何用一套代码横扫 Web、桌面、移动与后端?
前端·rust·开源
DisonTangor4 小时前
阿里Qwen开源Qwen3-VL-Embedding 和 Qwen3-VL-Reranker
人工智能·搜索引擎·开源·aigc·embedding
搬砖的kk6 小时前
Cordova 适配鸿蒙系统(OpenHarmony) 全解析:技术方案、环境搭建与实战开发
华为·开源·harmonyos
猫头虎6 小时前
2026最新|GitHub 启用双因素身份验证 2FA 教程:TOTP.app 一键生成动态验证码(新手小白图文实操)
git·开源·gitlab·github·开源软件·开源协议·gitcode
REDcker6 小时前
Mach3、Mach4与开源数控软件对比分析
开源·加工·数控·机床·mach3·mach4·数控加工
java_logo6 小时前
Dify 开源 LLM 应用开发平台企业级 Docker Compose 部署手册
docker·容器·开源·dify部署·dify部署文档·dify部署方案·dify部署教程
_小苔藓_6 小时前
混合Token与LoRA结合Qwen3-VL高效微调(代码开源)
深度学习·开源·大模型·微调·多模态
武汉唯众智创6 小时前
开源技术应用专业全场景实训平台,构建实训新生态
开源·开源技术·开源技术实训室·开源技术实验室·开源技术实训平台
AllData公司负责人8 小时前
AllData数据中台-数据同步平台集成开源项目Seatunnel-Web,完成Mysql到Doris同步流程
数据库·mysql·开源