简体   繁体   English

如何在 React Native 中刷新 Web 视图?

[英]How can I Refresh Web View in React Native?

I have a webview as tab A and a todolist flatlist on tab B. If the user adds an entry to the flatlist on tab B, i want the tab A webview to refresh.我在选项卡 A 上有一个 webview,在选项卡 B 上有一个 todolist 平面列表。如果用户在选项卡 B 上的平面列表中添加了一个条目,我希望刷新选项卡 A webview。

I couldn't find any .refresh() or reload() methods on the webview control https://facebook.github.io/react-native/docs/webview.html我在 webview 控件https://facebook.github.io/react-native/docs/webview.html上找不到任何.refresh()reload()方法

Any ideas how to accomplish this?任何想法如何完成这个?

Well I reload WebView by doing following:好吧,我通过执行以下操作重新加载WebView

render() {
  let WebViewRef;
  return (
    <View style={Style1.container}>
      <WebView
        ref={WEBVIEW_REF => (WebViewRef = WEBVIEW_REF)}
        source={{ uri: this.state.site }}
        renderLoading={this.ActivityIndicatorLoadingView}
        startInLoadingState={true}
      />
      <Button title="Reload Me!" onpress={() => { WebViewRef && WebViewRef.reload(); }} />
    </View>
  )
}

In this code I Declare Reference Variable WebViewRef first then assign this to WebView as ref={WEBVIEW_REF => (WebViewRef = WEBVIEW_REF)} and then call this reference for reload() as ()=>{ WebViewRef && WebViewRef.reload();}在此代码中,我首先声明引用变量WebViewRef然后将其分配给WebViewref={WEBVIEW_REF => (WebViewRef = WEBVIEW_REF)} ,然后将此引用为reload()调用为()=>{ WebViewRef && WebViewRef.reload();}

You can set a key to the webview您可以为 webview 设置一个键

key={this.state.key}

and then you can reload it by updating the state然后你可以通过更新状态重新加载它

this.setState({ key: this.state.key + 1 }); 

The react-native-community/react-native-webview component has a .reload() method on the ref. react-native-community/react-native-webview组件在 ref 上有一个.reload()方法。

const webViewRef = useRef();
// ...
<WebView ref={(ref) => webViewRef.current = ref} ... />
// ... 

Then call webViewRef.current.reload();然后调用webViewRef.current.reload(); to reload.重新加载。

See react-native-webview/docs/Reference.md#reload请参阅react-native-webview/docs/Reference.md#reload

I ended up using a dummy query parameter to signal a refresh of the web view:我最终使用了一个虚拟查询参数来表示 Web 视图的刷新:

In Tab B, I dispatch a change which changes "latestItemId" in the global state.在选项卡 B 中,我调度了一个更改,该更改更改了全局状态中的“latestItemId”。 In Tab A, I use mapStateToProps which maps to <WebView source={{uri:URL?latestItemId=${latestItemId}}} /> in the render method.在选项卡 A 中,我使用 mapStateToProps 映射到渲染方法中的<WebView source={{uri:URL?latestItemId=${latestItemId}}} /> This causes it to think it's a new url and reload it.这会导致它认为这是一个新的 url 并重新加载它。

Reload was not working on my end.重新加载对我来说不起作用。

If you want to refresh on focus change you can use the hook useFocusEffect of react navigation and in the unmount clean the URL used in the webview.如果您想刷新焦点更改,您可以使用 react 导航的钩子 useFocusEffect 并在卸载时清除 webview 中使用的 URL。 Then in the initialize you need to set that again.然后在初始化中你需要再次设置。 Maybe using aa useState.也许使用aa useState。

 useFocusEffect( useCallback(() => { setUrl(url!); return () => { setUrl(undefined); }; }, [url]), );

In my case I have source={{html}} so refresh() won't work in that situation.在我的例子中,我有source={{html}}所以refresh()在那种情况下不起作用。 However, in my case I am still able to inject javascript so I can set some properties specifically document.styles.body.color to match dark and light mode.但是,在我的例子中,我仍然能够注入 javascript,因此我可以设置一些属性,特别是document.styles.body.color以匹配暗模式和亮模式。

  const fg = colorScheme === "light" ? "black" : "white";
  const webViewRef = createRef<WebView>();
  useEffect(() => {
    webViewRef.current?.injectJavaScript(`
      document.body.style.color='${fg}';
      true
    `);
  }, [fg]);

...
  <WebView
    ref={webViewRef}
    originWhitelist={["*"]}
    style={{
      height: 200,
      backgroundColor: "transparent",
    }}
    onMessage={() => {}}
    javaScriptEnabled={true}
    injectedJavaScript={`
      document.body.style.color='${fg}';
      true;`}
    source={{ html }}
  />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM