Skip to content

Commit 0f6117a

Browse files
guycavshkl
authored andcommitted
More work Context api example
Played around with context api, tried to get the wrapped component to rerender after global context was updated - this is what I came up with. Note: this commit makes the playground app use a newer js core as the js core which shipped with RN on Android is missing javascript Proxy. Closes wix#4517
1 parent 04cd462 commit 0f6117a

File tree

6 files changed

+55
-15
lines changed

6 files changed

+55
-15
lines changed

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
"@types/react": "16.x.x",
6767
"@types/react-native": "0.57.7",
6868
"@types/react-test-renderer": "16.x.x",
69+
"jsc-android": "236355.x.x",
6970
"detox": "9.0.6",
7071
"handlebars": "4.x.x",
7172
"jest": "23.x.x",
@@ -75,7 +76,7 @@
7576
"react-native-typescript-transformer": "^1.2.10",
7677
"react-native-view-overflow": "0.0.3",
7778
"react-redux": "5.x.x",
78-
"react-test-renderer": "16.6.3",
79+
"react-test-renderer": "16.6.1",
7980
"redux": "3.x.x",
8081
"remx": "2.x.x",
8182
"semver": "5.x.x",

playground/android/app/build.gradle

+6
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@ configurations.all {
5454
}
5555
}
5656

57+
configurations.all {
58+
resolutionStrategy {
59+
force 'org.webkit:android-jsc:r236355'
60+
}
61+
}
62+
5763
dependencies {
5864
implementation fileTree(dir: 'libs', include: ['*.jar'])
5965
implementation 'com.android.support:design:28.0.0'

playground/android/build.gradle

+1
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,6 @@ allprojects {
2727
}
2828
maven { url 'https://jitpack.io' }
2929
maven { url 'https://oss.sonatype.org/content/repositories/snapshots' }
30+
maven { url "$rootDir/../../node_modules/jsc-android/dist" }
3031
}
3132
}

playground/src/context/index.js

+27-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,32 @@
11
const React = require('react');
2-
const titleContext = React.createContext('Default title from Context');
2+
let ctx = {
3+
title: 'Title from global context',
4+
count: 0
5+
};
6+
7+
const stateAwareContext = (component) =>
8+
new Proxy(ctx, {
9+
set: function (obj, prop, value) {
10+
obj[prop] = value;
11+
component.setState({ context: stateAwareContext(component) });
12+
return true;
13+
}
14+
});
15+
16+
const GlobalContext = React.createContext({});
17+
class ContextProvider extends React.Component {
18+
state = {context :stateAwareContext(this)};
19+
render() {
20+
return (
21+
<GlobalContext.Provider value={this.state.context}>
22+
{this.props.children}
23+
</GlobalContext.Provider>
24+
);
25+
}
26+
}
327

428
module.exports = {
5-
TitleContext: titleContext,
29+
ContextProvider,
30+
GlobalContext,
631
Context: React.createContext('Default value from Context')
732
}

playground/src/screens/ContextScreen.js

+13-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const React = require('react');
2-
const { View, Text } = require('react-native');
2+
const { View, Text, Button } = require('react-native');
33
const testIDs = require('../testIDs');
4-
const { TitleContext, Context } = require('../context');
4+
const { GlobalContext, Context } = require('../context');
55

66
class ContextScreen extends React.Component {
77
static contextType = Context;
@@ -28,11 +28,18 @@ class ContextScreen extends React.Component {
2828
</View>
2929
<View style={{ flexDirection: 'row', alignItems: 'center' }}>
3030
<Text style={styles.h2}>Provider value: </Text>
31-
<TitleContext.Consumer>
32-
{title => (
33-
<Text style={styles.h2} testID={testIDs.CENTERED_TEXT_HEADER}>{title}</Text>
31+
<GlobalContext.Consumer>
32+
{ctx => (
33+
<Text style={styles.h2} testID={testIDs.CENTERED_TEXT_HEADER}>{ctx.title}</Text>
3434
)}
35-
</TitleContext.Consumer>
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>
3643
</View>
3744
</View>
3845
);

playground/src/screens/index.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ const KeyboardScreen = require('./KeyboardScreen');
2929
const BottomTabSideMenuScreen = require('./complexlayouts/BottomTabSideMenuScreen');
3030
const FlatListScreen = require('./FlatListScreen');
3131
const ContextScreen = require('./ContextScreen');
32-
const { TitleContext } = require('../context');
32+
const { ContextProvider } = require('../context');
3333

3434
function registerScreens() {
3535
Navigation.registerComponent(`navigation.playground.CustomTransitionDestination`, () => CustomTransitionDestination);
@@ -40,12 +40,12 @@ function registerScreens() {
4040
Navigation.registerComponent(`navigation.playground.LifecycleScreen`, () => LifecycleScreen);
4141
Navigation.registerComponent(`navigation.playground.StaticLifecycleOverlay`, () => StaticLifecycleOverlay);
4242
Navigation.registerComponent(`navigation.playground.TextScreen`, () => TextScreen);
43-
Navigation.registerComponent(`navigation.playground.PushedScreen`, () => PushedScreen);
44-
Navigation.registerComponent('navigation.playground.ContextScreen', () => (props) => (
45-
<TitleContext.Provider value={'Title from Provider'}>
43+
Navigation.registerComponent('navigation.playground.PushedScreen', () => PushedScreen);
44+
Navigation.registerComponent('navigation.playground.ContextScreen',() => (props) =>
45+
<ContextProvider>
4646
<ContextScreen {...props} />
47-
</TitleContext.Provider>
48-
), () => ContextScreen);
47+
</ContextProvider>,
48+
() => ContextScreen);
4949
Navigation.registerComponent(`navigation.playground.OptionsScreen`, () => OptionsScreen);
5050
Navigation.registerComponent(`navigation.playground.OrientationSelectScreen`, () => OrientationSelectScreen);
5151
Navigation.registerComponent(`navigation.playground.OrientationDetectScreen`, () => OrientationDetectScreen);

0 commit comments

Comments
 (0)