Pārlūkot izejas kodu

Remove Proxy from React.Context example

Hermes doesn't support Proxy ¯\_(ツ)_/¯
Guy Carmeli 5 gadus atpakaļ
vecāks
revīzija
a003f75fc1

+ 10
- 11
playground/src/context/index.js Parādīt failu

@@ -1,21 +1,20 @@
1 1
 const React = require('react');
2
-let ctx = {
2
+const _context = {
3 3
   title: 'Title from global context',
4 4
   count: 0
5 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
-  });
6
+const contextWrapper = (component) => ({
7
+  ..._context,
8
+  incrementCount: () => {
9
+    _context.count++;
10
+    component.setState({ context: contextWrapper(component) })
11
+  }
12
+});
15 13
 
16 14
 const GlobalContext = React.createContext({});
17 15
 class ContextProvider extends React.Component {
18
-  state = {context :stateAwareContext(this)};
16
+  state = { context: contextWrapper(this) }
17
+
19 18
   render() {
20 19
     return (
21 20
       <GlobalContext.Provider value={this.state.context}>

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

@@ -24,7 +24,7 @@ class ContextScreen extends React.Component {
24 24
           {ctx => <Text style={styles.text}>Provider value: {ctx.title}</Text>}
25 25
         </GlobalContext.Consumer>
26 26
         <GlobalContext.Consumer>
27
-          {ctx => <Button title={`clicked ${ctx.count}`} onPress={() => ctx.count++} />}
27
+          {ctx => <Button title={`clicked ${ctx.count}`} onPress={() => ctx.incrementCount()} />}
28 28
         </GlobalContext.Consumer>
29 29
       </Root>
30 30
     );