codemotionapps/react-native-dark-mode

Create a method that can be used outside of a component

Closed this issue · 2 comments

Hello,

I would like to know the light/dark mode outside of components. Say for analytics or API helpers.

Currently, if I try and use any of the methods included in the docs (ie: isDarkMode()) I get the error Hooks can only be called inside of the body of a function component.. I presume it is saying that it cannot apply hooks, which are needed to trigger a re-render on light/dark change, to non-components. It would be nice if the hooks could be enabled/disabled via an argument or if a new method could be exposed for non-component uses.

My work-around for now is to call useDarkMode() in my App.tsx render method and then store it in the redux store.

Thanks in advance,
Nick

I believe you can work around this by using initialMode and the eventEmitter to watch for changes and update your store accordingly. Am about to try this myself.

Since Appearance landed in master facebook/react-native@63fa3f2 I'm not going to add new functionality to this library. Will be also deprecating it as soon as our company moves on to using this new API.
Both using initialMode with eventEmitter or useDarkMode in some component are viable options. Use whatever one you're more comfortable with.