Pārlūkot izejas kodu

Update React Context API example

This is an example of how to use React’s Context API and RNN. This solution isn’t ideal, hopefully someone
would be able to build upon this example and improve it.
Guy Carmeli 5 gadus atpakaļ
vecāks
revīzija
1ca24f45b4

+ 17
- 37
playground/src/screens/ContextScreen.js Parādīt failu

@@ -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
 };

+ 2
- 0
playground/src/screens/NavigationScreen.js Parādīt failu

@@ -38,6 +38,7 @@ class NavigationScreen  extends React.Component {
38 38
         <Button label='External Component' testID={EXTERNAL_COMP_BTN} onPress={this.externalComponent} />
39 39
         <Button label='Static Events' testID={SHOW_STATIC_EVENTS_SCREEN} onPress={this.pushStaticEventsScreen} />
40 40
         <Button label='Orientation' testID={SHOW_ORIENTATION_SCREEN} onPress={this.orientation} />
41
+        <Button label='React Context API' onPress={this.pushContextScreen} />
41 42
         <Navigation.TouchablePreview
42 43
           touchableComponent={Button}
43 44
           onPressIn={this.preview}
@@ -52,6 +53,7 @@ class NavigationScreen  extends React.Component {
52 53
   externalComponent = () => Navigation.showModal(Screens.ExternalComponent);
53 54
   pushStaticEventsScreen = () => Navigation.showModal(Screens.EventsScreen)
54 55
   orientation = () => Navigation.showModal(Screens.Orientation);
56
+  pushContextScreen = () => Navigation.push(this, Screens.ContextScreen);
55 57
   preview = ({reactTag}) => {
56 58
     Navigation.push(this.props.componentId, {
57 59
       component: {

+ 1
- 0
playground/src/screens/Screens.js Parādīt failu

@@ -1,5 +1,6 @@
1 1
 module.exports = {
2 2
   ExternalComponent: 'ExternalComponent',
3
+  ContextScreen: 'ContextScreen',
3 4
   Pushed: 'Pushed',
4 5
   Layouts: 'Layouts',
5 6
   Options: 'Options',

+ 8
- 6
playground/src/screens/index.js Parādīt failu

@@ -9,8 +9,6 @@ const TopTabOptionsScreen = require('./TopTabOptionsScreen');
9 9
 const CustomTextButton = require('./CustomTextButton');
10 10
 const TopBarBackground = require('./TopBarBackground');
11 11
 const KeyboardScreen = require('./KeyboardScreen');
12
-const ContextScreen = require('./ContextScreen');
13
-const { ContextProvider } = require('../context');
14 12
 const Screens = require('./Screens');
15 13
 
16 14
 function registerScreens() {
@@ -41,14 +39,18 @@ function registerScreens() {
41 39
   Navigation.registerComponent(Screens.Search, () => require('./SearchScreen'));
42 40
   Navigation.registerComponent(Screens.ExternalComponent, () => require('./ExternalComponentScreen'));
43 41
 
44
-  Navigation.registerComponent(`navigation.playground.CustomTransitionDestination`, () => CustomTransitionDestination);
45
-  Navigation.registerComponent(`navigation.playground.CustomTransitionOrigin`, () => CustomTransitionOrigin);
46
-  Navigation.registerComponent(`navigation.playground.ScrollViewScreen`, () => ScrollViewScreen);
47
-  Navigation.registerComponent('navigation.playground.ContextScreen', () => (props) =>
42
+  const { ContextProvider } = require('../context');
43
+  const ContextScreen = require('./ContextScreen');
44
+  Navigation.registerComponent(Screens.ContextScreen, () => (props) =>
48 45
     <ContextProvider>
49 46
       <ContextScreen {...props} />
50 47
     </ContextProvider>,
51 48
     () => ContextScreen);
49
+
50
+  Navigation.registerComponent(`navigation.playground.CustomTransitionDestination`, () => CustomTransitionDestination);
51
+  Navigation.registerComponent(`navigation.playground.CustomTransitionOrigin`, () => CustomTransitionOrigin);
52
+  Navigation.registerComponent(`navigation.playground.ScrollViewScreen`, () => ScrollViewScreen);
53
+
52 54
   Navigation.registerComponent('navigation.playground.CustomDialog', () => CustomDialog);
53 55
   Navigation.registerComponent('navigation.playground.CustomDialogWithScroll', () => CustomDialogWithScroll);
54 56
   Navigation.registerComponent('navigation.playground.TopTabScreen', () => TopTabScreen);