如何根据设备主题实时更改状态栏背景?

I wrote the following code and I wanted whenever I toggle my device's dark or light mode the statusbar change as well, but I have to reload react native app from terminal to see change on statusbar backgroundenter image description here

const App = () => {
  const theme = useTheme();
  const colorScheme = Appearance.getColorScheme();
  const statusBarBackgroundColor =
    colorScheme === "dark" ? theme.colors.onSurface : theme.colors.surface;

  return (
    <StatusBar
      animated={true}
      backgroundColor={statusBarBackgroundColor}
      barStyle={colorScheme === "dark" ? "light-content" : "dark-content"}
      showHideTransition={statusBarTransition}
      hidden={hidden}
    />
  );
};

推荐答案

try usecolorScheme钩子:

import { useColorScheme } from 'react-native';

const colorScheme = useColorScheme()

React-native相关问答推荐

条件呈现Else语句不会将样式应用于我的组件

react 本机中的TextInput对齐中的长文本

ios 中节列表中的scrollToLocation 不起作用.在 Android 上,列表滚动回索引 0,但在 ios 上不会发生同样的情况

何时在 React Native 中使用辅助功能 Role='link'?

React Native - 动画宽度缩小

react-native 签名 [AppName]Tests 需要开发团队 ios

从 .apk 到 .aab 的本地构建版本react,如何将应用程序发送给客户端?

警告:API 'variant.getMergeAssets()' 已过时 / Android Studio 3.3

我如何知道我的代码是否作为 React Native 运行

React Native 上传到 S3 存储空间

如何在 mac 上卸载 react-native-cli?

在 React Native 中计算两个 lat+lng 坐标之间的距离?

react Navigation 在标题中使用图像?

react-native 从右到左

React Native Maps:标记图像不显示在 react-native-maps 中使用自定义标记

如何在 array.map 中的每个元素之后添加逗号,除了 React JSX 中的最后一个元素

React Nativedropped so far性能监视器计数不断增加

opneUrl react-native 链接调用,mailto

React Native 发送短信

无法在react-native中分配给# 的只读属性props