Skip to content

Commit

Permalink
refactor(rn tester app): change appearence example to hooks (#35114)
Browse files Browse the repository at this point in the history
Summary:
This pull request migrates the appearance example to using React Hooks.

## Changelog
[General] [Changed] - RNTester: Migrate Appearence to hooks

Pull Request resolved: #35114

Test Plan: The animation works exactly as it did as when it was a class component

Reviewed By: cortinico

Differential Revision: D41531005

Pulled By: cipolleschi

fbshipit-source-id: a864766a3bb58a7f0c2b9c4ed8f731ee84713b26
  • Loading branch information
Marcoo09 authored and facebook-github-bot committed Nov 28, 2022
1 parent 9087186 commit 22576fa
Showing 1 changed file with 21 additions and 32 deletions.
53 changes: 21 additions & 32 deletions packages/rn-tester/js/examples/Appearance/AppearanceExample.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,47 +9,36 @@
*/

import * as React from 'react';
import {useState, useEffect} from 'react';
import {Appearance, Text, useColorScheme, View} from 'react-native';
import type {AppearancePreferences} from 'react-native/Libraries/Utilities/NativeAppearance';
import type {EventSubscription} from 'react-native/Libraries/vendor/emitter/EventEmitter';
import {RNTesterThemeContext, themes} from '../../components/RNTesterTheme';

class ColorSchemeSubscription extends React.Component<
{...},
{colorScheme: ?string, ...},
> {
_subscription: ?EventSubscription;
function ColorSchemeSubscription() {
const [colorScheme, setScheme] = useState(Appearance.getColorScheme());

state: {colorScheme: ?string, ...} = {
colorScheme: Appearance.getColorScheme(),
};

componentDidMount() {
this._subscription = Appearance.addChangeListener(
useEffect(() => {
const subscription = Appearance.addChangeListener(
(preferences: AppearancePreferences) => {
const {colorScheme} = preferences;
this.setState({colorScheme});
const {colorScheme: scheme} = preferences;
setScheme(scheme);
},
);
}

componentWillUnmount() {
this._subscription?.remove();
}
return () => subscription?.remove();
}, [setScheme]);

render(): React.Node {
return (
<RNTesterThemeContext.Consumer>
{theme => {
return (
<ThemedContainer>
<ThemedText>{this.state.colorScheme}</ThemedText>
</ThemedContainer>
);
}}
</RNTesterThemeContext.Consumer>
);
}
return (
<RNTesterThemeContext.Consumer>
{theme => {
return (
<ThemedContainer>
<ThemedText>{colorScheme}</ThemedText>
</ThemedContainer>
);
}}
</RNTesterThemeContext.Consumer>
);
}

const ThemedContainer = (props: {children: React.Node}) => (
Expand All @@ -69,7 +58,7 @@ const ThemedContainer = (props: {children: React.Node}) => (
</RNTesterThemeContext.Consumer>
);

const ThemedText = (props: {children: React.Node}) => (
const ThemedText = (props: {children: React.Node | string}) => (
<RNTesterThemeContext.Consumer>
{theme => {
return <Text style={{color: theme.LabelColor}}>{props.children}</Text>;
Expand Down

0 comments on commit 22576fa

Please sign in to comment.