소스 검색

refactor App with Hooks for demo

iou90 5 년 전
부모
커밋
d266a6f1d5
1개의 변경된 파일117개의 추가작업 그리고 135개의 파일을 삭제
  1. 117
    135
      demo/App.js

+ 117
- 135
demo/App.js 파일 보기

@@ -1,4 +1,4 @@
1
-import React, { Component } from 'react';
1
+import React, { useState } from 'react';
2 2
 
3 3
 import { ScrollView, StyleSheet, Text, TouchableOpacity, Platform, Linking } from 'react-native';
4 4
 
@@ -16,147 +16,127 @@ import {
16 16
   inlineBodyStyle
17 17
 } from './config';
18 18
 
19
-export default class Explorer extends Component {
20
-  constructor(props) {
21
-    super(props);
22
-    this.state = {
23
-      heightHtml: autoHeightHtml0,
24
-      heightScript: autoDetectLinkScript,
25
-      heightStyle: null,
26
-      heightSize: {
27
-        height: 0,
28
-        width: 0
29
-      },
30
-      widthHtml: autoWidthHtml0,
31
-      widthScript: null,
32
-      widthStyle: inlineBodyStyle,
33
-      widthSize: {
34
-        height: 0,
35
-        width: 0
36
-      }
37
-    };
38
-  }
39
-
40
-  changeSource = () => {
41
-    this.setState(prevState => ({
42
-      widthHtml: prevState.widthHtml === autoWidthHtml0 ? autoWidthHtml1 : autoWidthHtml0,
43
-      heightHtml: prevState.heightHtml === autoHeightHtml0 ? autoHeightHtml1 : autoHeightHtml0
44
-    }));
45
-  };
19
+const Explorer = () => {
20
+  const [{ widthHtml, heightHtml }, setHtml] = useState(() => ({
21
+    widthHtml: autoWidthHtml0,
22
+    heightHtml: autoHeightHtml0
23
+  }));
24
+  const changeSource = () =>
25
+    setHtml({
26
+      widthHtml: widthHtml === autoWidthHtml0 ? autoWidthHtml1 : autoWidthHtml0,
27
+      heightHtml: heightHtml === autoHeightHtml0 ? autoHeightHtml1 : autoHeightHtml0
28
+    });
46 29
 
47
-  changeStyle = () => {
48
-    this.setState(prevState => ({
49
-      widthStyle: prevState.widthStyle == inlineBodyStyle ? style0 + inlineBodyStyle : inlineBodyStyle,
50
-      heightStyle: prevState.heightStyle == null ? style0 : null
51
-    }));
52
-  };
30
+  const [{ widthStyle, heightStyle }, setStyle] = useState(() => ({
31
+    heightStyle: null,
32
+    widthStyle: inlineBodyStyle
33
+  }));
34
+  const changeStyle = () =>
35
+    setStyle({
36
+      widthStyle: widthStyle == inlineBodyStyle ? style0 + inlineBodyStyle : inlineBodyStyle,
37
+      heightStyle: heightStyle == null ? style0 : null
38
+    });
53 39
 
54
-  changeScript = () => {
55
-    this.setState(prevState => ({
56
-      widthScript: prevState.widthScript !== autoWidthScript ? autoWidthScript : null,
40
+  const [{ widthScript, heightScript }, setScript] = useState(() => ({
41
+    heightScript: autoDetectLinkScript,
42
+    widthScript: null
43
+  }));
44
+  const changeScript = () =>
45
+    setScript({
46
+      widthScript: widthScript !== autoWidthScript ? autoWidthScript : null,
57 47
       heightScript:
58
-        prevState.heightScript !== autoDetectLinkScript ? autoDetectLinkScript : autoHeightScript + autoDetectLinkScript
59
-    }));
60
-  };
48
+        heightScript !== autoDetectLinkScript ? autoDetectLinkScript : autoHeightScript + autoDetectLinkScript
49
+    });
50
+
51
+  const [heightSize, setHeightSize] = useState(() => ({ height: 0, width: 0 }));
52
+  const [widthSize, setWidthSize] = useState(() => ({ height: 0, width: 0 }));
61 53
 
62
-  render() {
63
-    const {
64
-      heightHtml,
65
-      heightSize,
66
-      heightStyle,
67
-      heightScript,
68
-      widthHtml,
69
-      widthSize,
70
-      widthStyle,
71
-      widthScript
72
-    } = this.state;
73
-    return (
74
-      <ScrollView
54
+  return (
55
+    <ScrollView
56
+      style={{
57
+        paddingTop: 45,
58
+        backgroundColor: 'lightyellow'
59
+      }}
60
+      contentContainerStyle={{
61
+        justifyContent: 'center',
62
+        alignItems: 'center'
63
+      }}
64
+    >
65
+      <AutoHeightWebView
66
+        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
+        }}
75
+        onSizeUpdated={heightSize => setHeightSize(heightSize)}
76
+        source={{ html: heightHtml }}
77
+        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
+        }}
95
+      />
96
+      <Text style={{ padding: 5 }}>
97
+        height: {heightSize.height}, width: {heightSize.width}
98
+      </Text>
99
+      <AutoHeightWebView
100
+        baseUrl={Platform.OS === 'android' ? 'file:///android_asset/webAssets/' : 'webAssets/'}
75 101
         style={{
76
-          paddingTop: 45,
77
-          backgroundColor: 'lightyellow'
102
+          marginTop: 15
78 103
         }}
79
-        contentContainerStyle={{
80
-          justifyContent: 'center',
81
-          alignItems: 'center'
104
+        enableBaseUrl
105
+        files={[
106
+          {
107
+            href: 'demo.css',
108
+            type: 'text/css',
109
+            rel: 'stylesheet'
110
+          }
111
+        ]}
112
+        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;
82 120
         }}
83
-      >
84
-        <AutoHeightWebView
85
-          customStyle={heightStyle}
86
-          onError={() => console.log('height on error')}
87
-          onLoad={() => console.log('height on load')}
88
-          onLoadStart={() => console.log('height on load start')}
89
-          onLoadEnd={() => console.log('height on load end')}
90
-          onShouldStartLoadWithRequest={result => {
91
-            console.log(result);
92
-            return true;
93
-          }}
94
-          onSizeUpdated={heightSize => this.setState({ heightSize })}
95
-          source={{ html: heightHtml }}
96
-          customScript={heightScript}
97
-          onMessage={event => {
98
-            const { data } = event.nativeEvent;
99
-            let messageData;
100
-            // maybe parse stringified JSON
101
-            try {
102
-              messageData = JSON.parse(data);
103
-            } catch (e) {
104
-              console.log(e.message);
105
-            }
106
-            if (typeof messageData === 'object') {
107
-              const { url } = messageData;
108
-              // check if this message concerns us
109
-              if (url && url.startsWith('http')) {
110
-                Linking.openURL(url).catch(error => console.error('An error occurred', error));
111
-              }
112
-            }
113
-          }}
114
-        />
115
-        <Text style={{ padding: 5 }}>
116
-          height: {heightSize.height}, width: {heightSize.width}
117
-        </Text>
118
-        <AutoHeightWebView
119
-          baseUrl={Platform.OS === 'android' ? 'file:///android_asset/webAssets/' : 'webAssets/'}
120
-          style={{
121
-            marginTop: 15
122
-          }}
123
-          enableBaseUrl
124
-          files={[
125
-            {
126
-              href: 'demo.css',
127
-              type: 'text/css',
128
-              rel: 'stylesheet'
129
-            }
130
-          ]}
131
-          customStyle={widthStyle}
132
-          onError={() => console.log('width on error')}
133
-          onLoad={() => console.log('width on load')}
134
-          onLoadStart={() => console.log('width on load start')}
135
-          onLoadEnd={() => console.log('width on load end')}
136
-          onShouldStartLoadWithRequest={result => {
137
-            console.log(result);
138
-            return true;
139
-          }}
140
-          onSizeUpdated={widthSize => this.setState({ widthSize })}
141
-          source={{ html: widthHtml }}
142
-          customScript={widthScript}
143
-        />
144
-        <Text style={{ padding: 5 }}>
145
-          height: {widthSize.height}, width: {widthSize.width}
146
-        </Text>
147
-        <TouchableOpacity onPress={this.changeSource} style={styles.button}>
148
-          <Text>change source</Text>
149
-        </TouchableOpacity>
150
-        <TouchableOpacity onPress={this.changeStyle} style={styles.button}>
151
-          <Text>change style</Text>
152
-        </TouchableOpacity>
153
-        <TouchableOpacity onPress={this.changeScript} style={[styles.button, { marginBottom: 100 }]}>
154
-          <Text>change script</Text>
155
-        </TouchableOpacity>
156
-      </ScrollView>
157
-    );
158
-  }
159
-}
121
+        onSizeUpdated={widthSize => setWidthSize(widthSize)}
122
+        source={{ html: widthHtml }}
123
+        customScript={widthScript}
124
+      />
125
+      <Text style={{ padding: 5 }}>
126
+        height: {widthSize.height}, width: {widthSize.width}
127
+      </Text>
128
+      <TouchableOpacity onPress={changeSource} style={styles.button}>
129
+        <Text>change source</Text>
130
+      </TouchableOpacity>
131
+      <TouchableOpacity onPress={changeStyle} style={styles.button}>
132
+        <Text>change style</Text>
133
+      </TouchableOpacity>
134
+      <TouchableOpacity onPress={changeScript} style={[styles.button, { marginBottom: 100 }]}>
135
+        <Text>change script</Text>
136
+      </TouchableOpacity>
137
+    </ScrollView>
138
+  );
139
+};
160 140
 
161 141
 const styles = StyleSheet.create({
162 142
   button: {
@@ -166,3 +146,5 @@ const styles = StyleSheet.create({
166 146
     padding: 5
167 147
   }
168 148
 });
149
+
150
+export default Explorer;