-
-
Notifications
You must be signed in to change notification settings - Fork 132
/
Copy pathapp.js
36 lines (34 loc) · 1.09 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import React from 'react';
import {View, Button} from 'react-native';
import EStyleSheet from 'react-native-extended-stylesheet';
import MyComponent from './component';
import darkTheme from './dark';
import lightTheme from './light';
EStyleSheet.build(lightTheme);
export default class extends React.Component {
constructor() {
super();
this.state = {
shouldRender: true
};
}
toggleTheme() {
const theme = EStyleSheet.value('$theme') === 'light' ? darkTheme : lightTheme;
EStyleSheet.build(theme);
// setState() called twice to re-render whole component tree
this.setState({shouldRender: false}, () => this.setState({shouldRender: true}));
}
render() {
if (this.state.shouldRender) {
const buttonTitle = EStyleSheet.value('$theme') === 'light' ? 'Set dark theme' : 'Set light theme';
return (
<View style={{flex: 1}}>
<MyComponent/>
<Button title={buttonTitle} onPress={() => this.toggleTheme()}/>
</View>
);
} else {
return null; // returning null is important to re-render component tree
}
}
}