question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Apparence turns light (for an instant) to when back and forth on iOS with SDK39

See original GitHub issue

Hi guys,

For some reason the useColorScheme on iOS returns “light” (for a moment) when the application goes to background and returns to foreground, I suppose that this is something that the SDK39 introduces because I had no problem with the previous version.

It could be that it is related to a facebook issue:

https://github.com/facebook/react-native/issues/28525

here I leave an video of the problem:

RPReplay_Final1603742498

Alternatively I tried with the native useColorTheme and had the same unwanted effect.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:11
  • Comments:32 (10 by maintainers)

github_iconTop GitHub Comments

19reactions
outaTiMEcommented, Nov 3, 2020

great @jasonaibrahim, i made few tweak usign lodash and better memoization to reduce size, at this moment the only way i know to “workaround” this issue is throttled the Appearance updates 😞

useColorScheme(delay = 250) {
  const [colorScheme, setColorScheme] = React.useState(
    Appearance.getColorScheme()
  );
  const onColorSchemeChange = React.useCallback(
    _.throttle(
      ({ colorScheme }) => {
        setColorScheme(colorScheme);
      },
      delay,
      {
        leading: false,
      }
    ),
    []
  );
  React.useEffect(() => {
    Appearance.addChangeListener(onColorSchemeChange);
    return () => {
      onColorSchemeChange.cancel();
      Appearance.removeChangeListener(onColorSchemeChange);
    };
  }, []);
  return colorScheme;
}

BTW, 250ms on throttle invocations works for me

8reactions
jasonaibrahimcommented, Oct 29, 2020

if anyone needs a workaround while this is sorted out, here it is as a hook that can be used in lieu of the rn useColorScheme hook. it throttles the color scheme change events so that the more recent one is used as the colorScheme value, since the first event seems to be incorrect but subsequent values are correct

import { Appearance, ColorSchemeName } from 'react-native';
import { useEffect, useRef, useState } from 'react';

export default function useColorScheme(delay = 500): NonNullable<ColorSchemeName> {
  const [colorScheme, setColorScheme] = useState(Appearance.getColorScheme());

  let timeout = useRef<NodeJS.Timeout | null>(null).current;

  useEffect(() => {
    Appearance.addChangeListener(onColorSchemeChange);

    return () => {
      resetCurrentTimeout();
      Appearance.removeChangeListener(onColorSchemeChange);
    };
  }, []);

  function onColorSchemeChange(preferences: Appearance.AppearancePreferences) {
    resetCurrentTimeout();

    timeout = setTimeout(() => {
      setColorScheme(preferences.colorScheme);
    }, delay);
  }

  function resetCurrentTimeout() {
    if (timeout) {
      clearTimeout(timeout);
    }
  }

  return colorScheme as NonNullable<ColorSchemeName>;
}

Read more comments on GitHub >

github_iconTop Results From Across the Web

Supporting Dark Mode in Your Interface - Apple Developer
In macOS and iOS, users can choose to adopt a system-wide light or dark appearance. The dark appearance, known as Dark Mode, implements...
Read more >
Light and dark modes - Expo Documentation
Learn how to support light and dark modes in your app.
Read more >
Implement dark mode switch in SwiftUI App - Stack Overflow
preferredColorScheme(isDarkMode ? .dark : .light) // tint on status ... once the app switches state (goes to the background and comes back).
Read more >
Dark mode - QuickBooks Design System
It analyzes your Figma file, converts colors for you, and can go back and forth between dark mode and light mode.
Read more >
How to automatically change between dark mode and light ...
This is how to set your iPhone to automatically change appearance between dark mode and light mode using shortcut automations on iOS 15!...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found