Bläddra i källkod

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

iou90kant 4 år sedan
förälder
incheckning
d628c8ac38
2 ändrade filer med 50 tillägg och 34 borttagningar
  1. 2
    1
      demo/.eslintrc.js
  2. 48
    33
      demo/App.js

+ 2
- 1
demo/.eslintrc.js Visa fil

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

+ 48
- 33
demo/App.js Visa fil

@@ -16,6 +16,43 @@ import {
16 16
   inlineBodyStyle
17 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 56
 const Explorer = () => {
20 57
   const [{ widthHtml, heightHtml }, setHtml] = useState(() => ({
21 58
     widthHtml: autoWidthHtml0,
@@ -64,34 +101,15 @@ const Explorer = () => {
64 101
     >
65 102
       <AutoHeightWebView
66 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 109
         onSizeUpdated={setHeightSize}
76 110
         source={{ html: heightHtml }}
77 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 114
       <Text style={{ padding: 5 }}>
97 115
         height: {heightSize.height}, width: {heightSize.width}
@@ -110,14 +128,11 @@ const Explorer = () => {
110 128
           }
111 129
         ]}
112 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 136
         onSizeUpdated={setWidthSize}
122 137
         source={{ html: widthHtml }}
123 138
         customScript={widthScript}