Parcourir la source

fix react hooks using

iou90 il y a 5 ans
Parent
révision
7cfb03f05c
2 fichiers modifiés avec 23 ajouts et 23 suppressions
  1. 12
    7
      autoHeightWebView/index.js
  2. 11
    16
      autoHeightWebView/utils.js

+ 12
- 7
autoHeightWebView/index.js Voir le fichier

@@ -8,7 +8,7 @@ import PropTypes from 'prop-types';
8 8
 
9 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 13
 const AutoHeightWebView = forwardRef((props, ref) => {
14 14
   let webView = useRef();
@@ -20,7 +20,7 @@ const AutoHeightWebView = forwardRef((props, ref) => {
20 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 25
   const [size, setSize] = useState(() => ({
26 26
     height: style && style.height ? style.height : 0,
@@ -48,7 +48,10 @@ const AutoHeightWebView = forwardRef((props, ref) => {
48 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 56
   const { width, height } = size;
54 57
   useEffect(
@@ -58,8 +61,9 @@ const AutoHeightWebView = forwardRef((props, ref) => {
58 61
         height,
59 62
         width
60 63
       }),
61
-    [width, height]
64
+    [width, height, onSizeUpdated]
62 65
   );
66
+
63 67
   return (
64 68
     <WebView
65 69
       {...props}
@@ -74,7 +78,7 @@ const AutoHeightWebView = forwardRef((props, ref) => {
74 78
         style
75 79
       ]}
76 80
       injectedJavaScript={script}
77
-      source={source}
81
+      source={currentSource}
78 82
     />
79 83
   );
80 84
 });
@@ -96,10 +100,11 @@ AutoHeightWebView.propTypes = {
96 100
   style: ViewPropTypes.style,
97 101
   customScript: PropTypes.string,
98 102
   customStyle: PropTypes.string,
103
+  zoomable: PropTypes.bool,
99 104
   // webview props
100 105
   originWhitelist: PropTypes.arrayOf(PropTypes.string),
101 106
   onMessage: PropTypes.func,
102
-  zoomable: PropTypes.bool,
107
+  source: PropTypes.object
103 108
 };
104 109
 
105 110
 let defaultProps = {
@@ -107,7 +112,7 @@ let defaultProps = {
107 112
   showsHorizontalScrollIndicator: false,
108 113
   originWhitelist: ['*'],
109 114
   baseUrl: 'web/',
110
-  zoomable: true,
115
+  zoomable: true
111 116
 };
112 117
 
113 118
 Platform.OS === 'android' &&

+ 11
- 16
autoHeightWebView/utils.js Voir le fichier

@@ -31,10 +31,12 @@ const updateSizeWithMessage = element =>
31 31
 const makeScalePageToFit = zoomable => `
32 32
 var meta = document.createElement('meta'); 
33 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 42
   if (!document.getElementById("rnahw-wrapper")) {
@@ -95,9 +97,8 @@ ${script}
95 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 102
   script = files && files.length > 0 ? appendFilesToHead({ files, script }) : script;
102 103
   script = appendStylesToHead({ style: customStyle, script });
103 104
   customScript && (script = customScript + script);
@@ -113,25 +114,19 @@ export const isSizeChanged = ({ height, previousHeight, width, previousWidth })
113 114
     return;
114 115
   }
115 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 119
 export const getMemoResult = props => {
124 120
   const { source, baseUrl } = props;
125 121
   const script = getScript(props);
122
+  let currentSource = baseUrl ? { baseUrl } : {};
126 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 126
   } else {
131
-    let currentSource = Object.assign({}, source);
132
-    baseUrl && Object.assign(currentSource, { baseUrl });
127
+    Object.assign(currentSource, source);
133 128
     return {
134
-      source: currentSource,
129
+      currentSource,
135 130
       script
136 131
     };
137 132
   }