react-native-darkmode
is very light weight library which provides user's mode of preference (DARK/LIGHT) in mobile device.
Based on device mode user can change application mode.
- React Native 0.59.10 or higher
- Xcode 11 and above
- iOS 13 and above
- Android 10 and above
- Previous android versions which supports dark mode
npm install react-native-darkmode --save
react-native link react-native-darkmode
refer this link for manual installation.
Android app restarts when dark mode changes.
You can prevent Android app from restarting when dark mode changes by performing simple one native change.
You just need to append uiMode
to the android:configChanges
prop of in AndroidManifest.xml
.
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize|uiMode" //added here
android:windowSoftInputMode="adjustResize">
import DarkMode from 'react-native-darkmode';
Prop | Type | Description | Values |
---|---|---|---|
initialMode | String | indicates initial appearance | DARK/LIGHT |
supportsDarkMode | Boolean | indicates whether app supports Dark mode or not | true/false |
Prop | Type | Description | Values |
---|---|---|---|
onModeChange | String | emits event while user changes appearance mode | DARK/LIGHT |
import DarkMode, { initialMode, supportsDarkMode } from 'react-native-darkmode';
import { NativeEventEmitter } from 'react-native';
console.log('supportsDarkMode', supportsDarkMode);
console.log('initialMode', initialMode);
const DarkModeChange = new NativeEventEmitter(DarkMode);
componentDidMount(){
DarkModeChange.addListener('onModeChange', (event) => {
console.log('onModeChange', event);
// perform action on mode change
})
}
componentWillUnmount() {
// remove the listener
DarkModeChange.removeListener();
}