百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
R

react-native-dark-mode

> 前端框架
开源

在 React Native 中检测暗模式

365 stars0 点赞3 次浏览
访问官网GitHub

工具介绍

在 React Native 中检测暗模式

react-native-dark-mode

⚠️ DEPRECATED ⚠️

Please upgrade to React Native 0.62 and react-native-dynamic (in 1.0.0 all high level APIs are available, intentionally undocumented, will be removed in 2.0.0).

     

Installation

Prevent Android app from restarting when dark mode changes

You must append |uiMode to the android:configChanges prop of `` in AndroidManifest.xml. Example:

diff
diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml
--- a/android/app/src/main/AndroidManifest.xml
+++ b/android/app/src/main/AndroidManifest.xml
@@ -13,7 +13,7 @@
                
                        
                                

(iOS) Make sure you don't have UIUserInterfaceStyle in Info.plist

In iOS you can force the operating system to display your app always in light mode or dark mode by specifying it in Info.plist. If you did that in the past this module will not work.

React Native 0.60 or above

bash
npm install react-native-dark-mode
cd ios && pod install # for iOS

React Native 0.59

bash
npm install react-native-dark-mode
react-native link react-native-dark-mode

Usage

High level APIs

useDarkMode

Returns a boolean. true when dark mode is on.

javascript
import { useDarkMode } from 'react-native-dark-mode'

function Component() {
	const isDarkMode = useDarkMode()
	return 
}

useDarkModeContext

Returns dark or light.

javascript
import { useDarkModeContext } from 'react-native-dark-mode'

const backgroundColors = {
	light: 'white',
	dark: 'black',
}

function Component() {
	const mode = useDarkModeContext()
	const backgroundColor = backgroundColors[mode]
	return 
}

DynamicStyleSheet, DynamicValue and useDynamicStyleSheet

Just like StyleSheet but with support for dynamic values.

javascript
import { DynamicStyleSheet, DynamicValue, useDynamicStyleSheet } from 'react-native-dark-mode'

const dynamicStyles = new DynamicStyleSheet({
	container: {
		backgroundColor: new DynamicValue('white', 'black'),
		flex: 1,
	},
	text: {
		color: new DynamicValue('black', 'white'),
		textAlign: 'center',
	},
})

function Component() {
	const styles = useDynamicStyleSheet(dynamicStyles)

	return (
		
			My text
		
	)
}

DarkModeProvider

Allows you to set a specific mode for children.

javascript
import { DarkModeProvider } from 'react-native-dark-mode'

function MyScreen() {
	return (
		<>
			{/* will be rendered using dark theme */}
			
				
			

			{/* will be rendered using light theme */}
			
				
			

			{/* will be rendered using current theme */}
			
		
	)
}

It is recommended to wrap your application in a DarkModeProvider without a mode prop to observe a performance improvement.

javascript
function App() {
	return (
		
			{/* ... */}
		
	)
}

useDynamicValue

Returns the appropriate value depending on the theme. You can either pass a DynamicValue or just two arguments.

javascript
import { DynamicValue, useDynamicValue } from 'react-native-dark-mode'
const lightLogo = require('./light.png')
const darkLogo = require('./dark.png')
const logoUri = new DynamicValue(lightLogo, darkLogo)

function Logo() {
	const source = useDynamicValue(logoUri)
	return 
}
javascript
import { useDynamicValue } from 'react-native-dark-mode'

function Input() {
	const placeholderColor = useDynamicValue('black', 'white')
	return 
}

Low level APIs

initialMode

This is the initial mode that the app started in.

javascript
import { initialMode } from 'react-native-dark-mode'

console.log('App started in', initialMode, 'mode')

eventEmitter

Allows you to subscribe to changes in the mode.

javascript
import { eventEmitter } from 'react-native-dark-mode'

eventEmitter.on('currentModeChanged', newMode => {
	console.log('Switched to', newMode, 'mode')
})

Requirements

iOS

  • Xcode 11
  • React Native 0.59.9 or higher
  • iOS 13 to see it in action

Android

  • Android 10 or Android Auto to see it in action

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

TypeScriptandroiddark-modeiosreact

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类前端框架
定价开源

> 相关工具

R
React
用于构建用户界面的 JavaScript 库
V
Vue.js
渐进式 JavaScript 框架
N
Next.js
基于 React 的全栈 Web 框架