Skip to content

Commit

Permalink
refactor: increase useColorScheme subscription efficiency
Browse files Browse the repository at this point in the history
  • Loading branch information
vonovak committed Jun 21, 2023
1 parent 5f7c5a8 commit a887171
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions packages/react-native/Libraries/Utilities/useColorScheme.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,16 @@
import type {ColorSchemeName} from './NativeAppearance';

import Appearance from './Appearance';
import {useSyncExternalStore} from 'react';
import {useCallback, useSyncExternalStore} from 'react';

export default function useColorScheme(): ?ColorSchemeName {
const subscribe = useCallback(callback => {
const appearanceSubscription = Appearance.addChangeListener(callback);
return appearanceSubscription.remove;
}, [])

return useSyncExternalStore(
callback => {
const appearanceSubscription = Appearance.addChangeListener(callback);
return () => appearanceSubscription.remove();
},
() => Appearance.getColorScheme(),
subscribe,
Appearance.getColorScheme,
);
}

0 comments on commit a887171

Please sign in to comment.