|
@@ -1,6 +1,6 @@
|
1
|
1
|
const React = require('react');
|
2
|
|
-const { View, Text, Button } = require('react-native');
|
3
|
|
-const testIDs = require('../testIDs');
|
|
2
|
+const { Text, Button } = require('react-native');
|
|
3
|
+const Root = require('../components/Root');
|
4
|
4
|
const { GlobalContext, Context } = require('../context');
|
5
|
5
|
|
6
|
6
|
class ContextScreen extends React.Component {
|
|
@@ -10,10 +10,7 @@ class ContextScreen extends React.Component {
|
10
|
10
|
return {
|
11
|
11
|
topBar: {
|
12
|
12
|
title: {
|
13
|
|
- text: 'My Screen'
|
14
|
|
- },
|
15
|
|
- background: {
|
16
|
|
- color: 'red'
|
|
13
|
+ text: 'React Context API'
|
17
|
14
|
}
|
18
|
15
|
}
|
19
|
16
|
};
|
|
@@ -21,27 +18,15 @@ class ContextScreen extends React.Component {
|
21
|
18
|
|
22
|
19
|
render() {
|
23
|
20
|
return (
|
24
|
|
- <View style={styles.root}>
|
25
|
|
- <View style={{ flexDirection: 'row', alignItems: 'center', marginBottom: 10 }}>
|
26
|
|
- <Text style={styles.h2}>Default value: </Text>
|
27
|
|
- <Text style={styles.h2} testID={testIDs.CENTERED_TEXT_HEADER}>{this.context}</Text>
|
28
|
|
- </View>
|
29
|
|
- <View style={{ flexDirection: 'row', alignItems: 'center' }}>
|
30
|
|
- <Text style={styles.h2}>Provider value: </Text>
|
31
|
|
- <GlobalContext.Consumer>
|
32
|
|
- {ctx => (
|
33
|
|
- <Text style={styles.h2} testID={testIDs.CENTERED_TEXT_HEADER}>{ctx.title}</Text>
|
34
|
|
- )}
|
35
|
|
- </GlobalContext.Consumer>
|
36
|
|
- </View>
|
37
|
|
- <View>
|
38
|
|
- <GlobalContext.Consumer>
|
39
|
|
- {ctx => (
|
40
|
|
- <Button title={`clicked ${ctx.count}`} onPress={() => ctx.count++}/>
|
41
|
|
- )}
|
42
|
|
- </GlobalContext.Consumer>
|
43
|
|
- </View>
|
44
|
|
- </View>
|
|
21
|
+ <Root style={styles.root}>
|
|
22
|
+ <Text style={styles.text}>Default value: {this.context}</Text>
|
|
23
|
+ <GlobalContext.Consumer>
|
|
24
|
+ {ctx => <Text style={styles.text}>Provider value: {ctx.title}</Text>}
|
|
25
|
+ </GlobalContext.Consumer>
|
|
26
|
+ <GlobalContext.Consumer>
|
|
27
|
+ {ctx => <Button title={`clicked ${ctx.count}`} onPress={() => ctx.count++} />}
|
|
28
|
+ </GlobalContext.Consumer>
|
|
29
|
+ </Root>
|
45
|
30
|
);
|
46
|
31
|
}
|
47
|
32
|
}
|
|
@@ -50,17 +35,12 @@ module.exports = ContextScreen;
|
50
|
35
|
|
51
|
36
|
const styles = {
|
52
|
37
|
root: {
|
53
|
|
- flexGrow: 1,
|
54
|
38
|
justifyContent: 'center',
|
55
|
|
- alignItems: 'center',
|
56
|
|
- backgroundColor: '#f5fcff'
|
|
39
|
+ alignItems: 'center'
|
57
|
40
|
},
|
58
|
|
- h1: {
|
59
|
|
- fontSize: 24,
|
60
|
|
- textAlign: 'center'
|
61
|
|
- },
|
62
|
|
- h2: {
|
63
|
|
- fontSize: 12,
|
|
41
|
+ text: {
|
|
42
|
+ fontSize: 14,
|
64
|
43
|
textAlign: 'center',
|
65
|
|
- },
|
|
44
|
+ marginBottom: 8
|
|
45
|
+ }
|
66
|
46
|
};
|