mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
305b4253c2
Summary: Changes `Appearance.addChangeListener` to return an `EventSubscription` object that has a `remove()` method on it. In an upcoming commit, calling `Appearance.removeChangeListener` will lead to a deprecation warning. Changelog: [General][Change] - `Appearance.addChangeListener` now returns an `EventSubscription`. Reviewed By: kacieb Differential Revision: D26696388 fbshipit-source-id: d0bdeffff3a2a366b3c11b6dc1417dfb2f1455c2
32 lines
828 B
JavaScript
32 lines
828 B
JavaScript
/**
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*
|
|
* @format
|
|
* @flow strict-local
|
|
*/
|
|
|
|
import {useMemo} from 'react';
|
|
import {useSubscription} from 'use-subscription';
|
|
import Appearance from './Appearance';
|
|
import type {ColorSchemeName} from './NativeAppearance';
|
|
|
|
export default function useColorScheme(): ?ColorSchemeName {
|
|
const subscription = useMemo(
|
|
() => ({
|
|
getCurrentValue: () => Appearance.getColorScheme(),
|
|
subscribe: callback => {
|
|
const appearanceSubscription = Appearance.addChangeListener(callback);
|
|
return () => {
|
|
appearanceSubscription.remove();
|
|
};
|
|
},
|
|
}),
|
|
[],
|
|
);
|
|
|
|
return useSubscription(subscription);
|
|
}
|