codemotionapps/react-native-dark-mode

Context is always light

RobertjanSchipper opened this issue · 9 comments

Hi,

My App is always in "light" mode.

I'm using <DarkModeProvider /> in my App.js wrapped around my <App /> . Further down a component I'm using DarkModeContext to set a static contextType, so I can use it like this:

const isDarkMode = this.context === 'dark'

But for some strange reason the context / mode is always "light". If I don't set the mode to dark programatically, the mode won't get dark.

App.js

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

<DarkModeProvider>
    <AppNavigator />
</DarkModeProvider>

Component.js

import { DarkModeContext } from 'react-native-dark-mode'

class Name extends Component {
    static contextType = DarkModeContext;

    render() {
        const isDarkMode = this.context === 'dark'

        return (
            {console.log('isDark', isDarkMode)} <-- results in "light"
        )
    }
}

What am I doing wrong?

What device are you testing on?

What is the value of supportsDarkMode?

I'm testing on iOS 13.4. How can I check the value of supportsDarkMode?

import { supportsDarkMode } from 'react-native-dark-mode'

console.log(supportsDarkMode)

iOS 13.4 is currently beta. Can you get your hands on a non beta version? Also iOS on a device or simulator?

Also can you try the event emitter and see if it works?

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

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

supportsDarkMode is true. I'm testing on a simulator. No at this moment I cannot get my hands on a non-beta version.

The eventEmitter isn't doing anything. I'm switching to dark mode under:
Settings -> Developer -> Dark Appearance

Oh, and when I check initialMode, the app is fired in "light" mode, while my phone is in dark mode.

Just downloaded Xcode 11.4 beta 3 and it seems to be fine. Were you able to test on another simulator/device? Are you running beta 3 and if not can you update? If you can't get it to run after switch device/Xcode can you try to run the example project in this repo and see if it's working?
image

supportsDarkMode = true ?

截屏2020-03-1216 03 47

@orzhtml I do not understand your question. supportsDarkMode is defined in initial-mode.ts.
image

I had this problem but the solution is pretty obvious in hindsight: if you are working on an existing app you have probably manually set an UIUserInterfaceStyle key in the Info.plist file. This forces the app to always use the specified value - you can just delete this key to restore the expected dynamic behaviour.

@jdmunro totally forgot about UIUserInterfaceStyle. I also mentioned it in the README. Thanks!