ソースを参照

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 6 年 前
コミット
1ca24f45b4
共有4 個のファイルを変更した28 個の追加43 個の削除を含む
  1. 17
    37
      playground/src/screens/ContextScreen.js
  2. 2
    0
      playground/src/screens/NavigationScreen.js
  3. 1
    0
      playground/src/screens/Screens.js
  4. 8
    6
      playground/src/screens/index.js

+ 17
- 37
playground/src/screens/ContextScreen.js ファイルの表示

1
 const React = require('react');
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
 const { GlobalContext, Context } = require('../context');
4
 const { GlobalContext, Context } = require('../context');
5
 
5
 
6
 class ContextScreen extends React.Component {
6
 class ContextScreen extends React.Component {
10
     return {
10
     return {
11
       topBar: {
11
       topBar: {
12
         title: {
12
         title: {
13
-          text: 'My Screen'
14
-        },
15
-        background: {
16
-          color: 'red'
13
+          text: 'React Context API'
17
         }
14
         }
18
       }
15
       }
19
     };
16
     };
21
 
18
 
22
   render() {
19
   render() {
23
     return (
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
 
35
 
51
 const styles = {
36
 const styles = {
52
   root: {
37
   root: {
53
-    flexGrow: 1,
54
     justifyContent: 'center',
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
     textAlign: 'center',
43
     textAlign: 'center',
65
-  },
44
+    marginBottom: 8
45
+  }
66
 };
46
 };

+ 2
- 0
playground/src/screens/NavigationScreen.js ファイルの表示

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

+ 1
- 0
playground/src/screens/Screens.js ファイルの表示

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

+ 8
- 6
playground/src/screens/index.js ファイルの表示

9
 const CustomTextButton = require('./CustomTextButton');
9
 const CustomTextButton = require('./CustomTextButton');
10
 const TopBarBackground = require('./TopBarBackground');
10
 const TopBarBackground = require('./TopBarBackground');
11
 const KeyboardScreen = require('./KeyboardScreen');
11
 const KeyboardScreen = require('./KeyboardScreen');
12
-const ContextScreen = require('./ContextScreen');
13
-const { ContextProvider } = require('../context');
14
 const Screens = require('./Screens');
12
 const Screens = require('./Screens');
15
 
13
 
16
 function registerScreens() {
14
 function registerScreens() {
41
   Navigation.registerComponent(Screens.Search, () => require('./SearchScreen'));
39
   Navigation.registerComponent(Screens.Search, () => require('./SearchScreen'));
42
   Navigation.registerComponent(Screens.ExternalComponent, () => require('./ExternalComponentScreen'));
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
     <ContextProvider>
45
     <ContextProvider>
49
       <ContextScreen {...props} />
46
       <ContextScreen {...props} />
50
     </ContextProvider>,
47
     </ContextProvider>,
51
     () => ContextScreen);
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
   Navigation.registerComponent('navigation.playground.CustomDialog', () => CustomDialog);
54
   Navigation.registerComponent('navigation.playground.CustomDialog', () => CustomDialog);
53
   Navigation.registerComponent('navigation.playground.CustomDialogWithScroll', () => CustomDialogWithScroll);
55
   Navigation.registerComponent('navigation.playground.CustomDialogWithScroll', () => CustomDialogWithScroll);
54
   Navigation.registerComponent('navigation.playground.TopTabScreen', () => TopTabScreen);
56
   Navigation.registerComponent('navigation.playground.TopTabScreen', () => TopTabScreen);