瀏覽代碼

fix react hooks using

iou90 5 年之前
父節點
當前提交
7cfb03f05c
共有 2 個檔案被更改,包括 23 行新增23 行删除
  1. 12
    7
      autoHeightWebView/index.js
  2. 11
    16
      autoHeightWebView/utils.js

+ 12
- 7
autoHeightWebView/index.js 查看文件

8
 
8
 
9
 import { WebView } from 'react-native-webview';
9
 import { WebView } from 'react-native-webview';
10
 
10
 
11
-import { getMemoInputProps, getMemoResult, getWidth, isSizeChanged } from './utils';
11
+import { getMemoResult, getWidth, isSizeChanged } from './utils';
12
 
12
 
13
 const AutoHeightWebView = forwardRef((props, ref) => {
13
 const AutoHeightWebView = forwardRef((props, ref) => {
14
   let webView = useRef();
14
   let webView = useRef();
20
     injectJavaScript: script => webView.current.injectJavaScript(script)
20
     injectJavaScript: script => webView.current.injectJavaScript(script)
21
   }));
21
   }));
22
 
22
 
23
-  const { style, onMessage, onSizeUpdated } = props;
23
+  const { style, onMessage, onSizeUpdated, source, baseUrl, files, customStyle, customScript, zoomable } = props;
24
 
24
 
25
   const [size, setSize] = useState(() => ({
25
   const [size, setSize] = useState(() => ({
26
     height: style && style.height ? style.height : 0,
26
     height: style && style.height ? style.height : 0,
48
     onMessage && onMessage(event);
48
     onMessage && onMessage(event);
49
   };
49
   };
50
 
50
 
51
-  const { source, script } = useMemo(() => getMemoResult(props), [getMemoInputProps(props)]);
51
+  const { currentSource, script } = useMemo(
52
+    () => getMemoResult({ source, baseUrl, files, customStyle, customScript, zoomable }),
53
+    [source, baseUrl, files, customStyle, customScript, zoomable]
54
+  );
52
 
55
 
53
   const { width, height } = size;
56
   const { width, height } = size;
54
   useEffect(
57
   useEffect(
58
         height,
61
         height,
59
         width
62
         width
60
       }),
63
       }),
61
-    [width, height]
64
+    [width, height, onSizeUpdated]
62
   );
65
   );
66
+
63
   return (
67
   return (
64
     <WebView
68
     <WebView
65
       {...props}
69
       {...props}
74
         style
78
         style
75
       ]}
79
       ]}
76
       injectedJavaScript={script}
80
       injectedJavaScript={script}
77
-      source={source}
81
+      source={currentSource}
78
     />
82
     />
79
   );
83
   );
80
 });
84
 });
96
   style: ViewPropTypes.style,
100
   style: ViewPropTypes.style,
97
   customScript: PropTypes.string,
101
   customScript: PropTypes.string,
98
   customStyle: PropTypes.string,
102
   customStyle: PropTypes.string,
103
+  zoomable: PropTypes.bool,
99
   // webview props
104
   // webview props
100
   originWhitelist: PropTypes.arrayOf(PropTypes.string),
105
   originWhitelist: PropTypes.arrayOf(PropTypes.string),
101
   onMessage: PropTypes.func,
106
   onMessage: PropTypes.func,
102
-  zoomable: PropTypes.bool,
107
+  source: PropTypes.object
103
 };
108
 };
104
 
109
 
105
 let defaultProps = {
110
 let defaultProps = {
107
   showsHorizontalScrollIndicator: false,
112
   showsHorizontalScrollIndicator: false,
108
   originWhitelist: ['*'],
113
   originWhitelist: ['*'],
109
   baseUrl: 'web/',
114
   baseUrl: 'web/',
110
-  zoomable: true,
115
+  zoomable: true
111
 };
116
 };
112
 
117
 
113
 Platform.OS === 'android' &&
118
 Platform.OS === 'android' &&

+ 11
- 16
autoHeightWebView/utils.js 查看文件

31
 const makeScalePageToFit = zoomable => `
31
 const makeScalePageToFit = zoomable => `
32
 var meta = document.createElement('meta'); 
32
 var meta = document.createElement('meta'); 
33
 meta.setAttribute('name', 'viewport'); 
33
 meta.setAttribute('name', 'viewport'); 
34
-meta.setAttribute('content', 'width=device-width, user-scalable=${zoomable ? 'yes' : 'no'}'); document.getElementsByTagName('head')[0].appendChild(meta);
34
+meta.setAttribute('content', 'width=device-width, user-scalable=${
35
+  zoomable ? 'yes' : 'no'
36
+}'); document.getElementsByTagName('head')[0].appendChild(meta);
35
 `;
37
 `;
36
 
38
 
37
-const getBaseScript = ({style, zoomable}) =>
39
+const getBaseScript = ({ style, zoomable }) =>
38
   `
40
   `
39
   ;
41
   ;
40
   if (!document.getElementById("rnahw-wrapper")) {
42
   if (!document.getElementById("rnahw-wrapper")) {
95
 </script>
97
 </script>
96
 `;
98
 `;
97
 
99
 
98
-const getScript = props => {
99
-  const { files, customStyle, customScript, style, zoomable } = props;
100
-  let script = getBaseScript({style, zoomable});
100
+const getScript = ({ files, customStyle, customScript, style, zoomable }) => {
101
+  let script = getBaseScript({ style, zoomable });
101
   script = files && files.length > 0 ? appendFilesToHead({ files, script }) : script;
102
   script = files && files.length > 0 ? appendFilesToHead({ files, script }) : script;
102
   script = appendStylesToHead({ style: customStyle, script });
103
   script = appendStylesToHead({ style: customStyle, script });
103
   customScript && (script = customScript + script);
104
   customScript && (script = customScript + script);
113
     return;
114
     return;
114
   }
115
   }
115
   return height !== previousHeight || width !== previousWidth;
116
   return height !== previousHeight || width !== previousWidth;
116
-}
117
-
118
-export const getMemoInputProps = props => {
119
-  const { files, customStyle, customScript, style, source, baseUrl } = props;
120
-  return [files, customStyle, customScript, style, source, baseUrl];
121
 };
117
 };
122
 
118
 
123
 export const getMemoResult = props => {
119
 export const getMemoResult = props => {
124
   const { source, baseUrl } = props;
120
   const { source, baseUrl } = props;
125
   const script = getScript(props);
121
   const script = getScript(props);
122
+  let currentSource = baseUrl ? { baseUrl } : {};
126
   if (source.html) {
123
   if (source.html) {
127
-    let currentSource = { html: getInjectedSource({ html: source.html, script }) };
128
-    baseUrl && Object.assign(currentSource, { baseUrl });
129
-    return { source: currentSource };
124
+    Object.assign(currentSource, { html: getInjectedSource({ html: source.html, script }) });
125
+    return { currentSource };
130
   } else {
126
   } else {
131
-    let currentSource = Object.assign({}, source);
132
-    baseUrl && Object.assign(currentSource, { baseUrl });
127
+    Object.assign(currentSource, source);
133
     return {
128
     return {
134
-      source: currentSource,
129
+      currentSource,
135
       script
130
       script
136
     };
131
     };
137
   }
132
   }