Browse Source

avoid creating arrow functions of callbacks each time on render for demo

iou90kant 5 years ago
parent
commit
d628c8ac38
2 changed files with 50 additions and 34 deletions
  1. 2
    1
      demo/.eslintrc.js
  2. 48
    33
      demo/App.js

+ 2
- 1
demo/.eslintrc.js View File

6
       {
6
       {
7
         singleQuote: true
7
         singleQuote: true
8
       }
8
       }
9
-    ]
9
+    ],
10
+    'comma-dangle': 'off'
10
   }
11
   }
11
 };
12
 };

+ 48
- 33
demo/App.js View File

16
   inlineBodyStyle
16
   inlineBodyStyle
17
 } from './config';
17
 } from './config';
18
 
18
 
19
+const onShouldStartLoadWithRequest = result => {
20
+  console.log(result);
21
+  return true;
22
+};
23
+
24
+const onError = ({ nativeEvent }) => console.error('WebView error: ', nativeEvent);
25
+
26
+const onMessage = event => {
27
+  const { data } = event.nativeEvent;
28
+  let messageData;
29
+  // maybe parse stringified JSON
30
+  try {
31
+    messageData = JSON.parse(data);
32
+  } catch (e) {
33
+    console.log(e.message);
34
+  }
35
+  if (typeof messageData === 'object') {
36
+    const { url } = messageData;
37
+    // check if this message concerns us
38
+    if (url && url.startsWith('http')) {
39
+      Linking.openURL(url).catch(error => console.error('An error occurred', error));
40
+    }
41
+  }
42
+};
43
+
44
+const onHeightLoadStart = () => console.log('height on load start');
45
+
46
+const onHeightLoad = () => console.log('height on load');
47
+
48
+const onHeightLoadEnd = () => console.log('height on load end');
49
+
50
+const onWidthLoadStart = () => console.log('width on load start');
51
+
52
+const onWidthLoad = () => console.log('width on load');
53
+
54
+const onWidthLoadEnd = () => console.log('width on load end');
55
+
19
 const Explorer = () => {
56
 const Explorer = () => {
20
   const [{ widthHtml, heightHtml }, setHtml] = useState(() => ({
57
   const [{ widthHtml, heightHtml }, setHtml] = useState(() => ({
21
     widthHtml: autoWidthHtml0,
58
     widthHtml: autoWidthHtml0,
64
     >
101
     >
65
       <AutoHeightWebView
102
       <AutoHeightWebView
66
         customStyle={heightStyle}
103
         customStyle={heightStyle}
67
-        onError={() => console.log('height on error')}
68
-        onLoad={() => console.log('height on load')}
69
-        onLoadStart={() => console.log('height on load start')}
70
-        onLoadEnd={() => console.log('height on load end')}
71
-        onShouldStartLoadWithRequest={result => {
72
-          console.log(result);
73
-          return true;
74
-        }}
104
+        onError={onError}
105
+        onLoad={onHeightLoad}
106
+        onLoadStart={onHeightLoadStart}
107
+        onLoadEnd={onHeightLoadEnd}
108
+        onShouldStartLoadWithRequest={onShouldStartLoadWithRequest}
75
         onSizeUpdated={setHeightSize}
109
         onSizeUpdated={setHeightSize}
76
         source={{ html: heightHtml }}
110
         source={{ html: heightHtml }}
77
         customScript={heightScript}
111
         customScript={heightScript}
78
-        onMessage={event => {
79
-          const { data } = event.nativeEvent;
80
-          let messageData;
81
-          // maybe parse stringified JSON
82
-          try {
83
-            messageData = JSON.parse(data);
84
-          } catch (e) {
85
-            console.log(e.message);
86
-          }
87
-          if (typeof messageData === 'object') {
88
-            const { url } = messageData;
89
-            // check if this message concerns us
90
-            if (url && url.startsWith('http')) {
91
-              Linking.openURL(url).catch(error => console.error('An error occurred', error));
92
-            }
93
-          }
94
-        }}
112
+        onMessage={onMessage}
95
       />
113
       />
96
       <Text style={{ padding: 5 }}>
114
       <Text style={{ padding: 5 }}>
97
         height: {heightSize.height}, width: {heightSize.width}
115
         height: {heightSize.height}, width: {heightSize.width}
110
           }
128
           }
111
         ]}
129
         ]}
112
         customStyle={widthStyle}
130
         customStyle={widthStyle}
113
-        onError={() => console.log('width on error')}
114
-        onLoad={() => console.log('width on load')}
115
-        onLoadStart={() => console.log('width on load start')}
116
-        onLoadEnd={() => console.log('width on load end')}
117
-        onShouldStartLoadWithRequest={result => {
118
-          console.log(result);
119
-          return true;
120
-        }}
131
+        onError={onError}
132
+        onLoad={onWidthLoad}
133
+        onLoadStart={onWidthLoadStart}
134
+        onLoadEnd={onWidthLoadEnd}
135
+        onShouldStartLoadWithRequest={onShouldStartLoadWithRequest}
121
         onSizeUpdated={setWidthSize}
136
         onSizeUpdated={setWidthSize}
122
         source={{ html: widthHtml }}
137
         source={{ html: widthHtml }}
123
         customScript={widthScript}
138
         customScript={widthScript}