简体   繁体   English

如何保持 react-native-webview 的多个实例打开(并安装),同时能够在它们之间导航(使用 react-navigation)

[英]How to keep multiple instances of react-native-webview open (and mounted) whilst being able to navigate between them (using react-navigation)

I have react-navigation set up in a react-native app like so.我在 react-native 应用程序中设置了 react-navigation ,就像这样。

 const Stack = createStackNavigator();

  return (
    <NavigationContainer>
      <Stack.Navigator>
        <Stack.Screen name="HomeScreen" component={HomeScreen}/>
        <Stack.Screen name="ScanScreen" component={ScanScreen} options={{ title: 'Add a new app'}} />
        <Stack.Screen name="AppScreen" component={AppScreen} options={{headerShown: false}}/>
      </Stack.Navigator>
    </NavigationContainer>

In the AppScreen I am displaying webpages from external sources using react-native-webview like so.在 AppScreen 中,我使用 react-native-webview 显示来自外部来源的网页,就像这样。

const AppScreen = ({ navigation, route }) => {

  const [uri, setUri] = useState();

  React.useEffect(() => {
    if (route.params?.uri) {
      setUri(route.params?.uri);
    }
  }, [route.params?.app]);

  return (
  <View>
    <WebView
      source={{ uri: uri }}
      allowsInlineMediaPlayback={true}
      scrollEnabled={false}
    />
  </View>
  );
};

The HomeScreen just shows a list of apps the user can view in the AppScreen. HomeScreen 仅显示用户可以在 AppScreen 中查看的应用程序列表。 Whenever the user navigates between these two screens however, the webview in the AppScreen gets unmounted which means the state of the application and any granted permissions to it are lost, is there a way to stop this from happening?然而,每当用户在这两个屏幕之间导航时,AppScreen 中的 webview 都会被卸载,这意味着应用程序的 state 及其任何授予的权限都会丢失,有没有办法阻止这种情况发生?

Additionally, is there a way to use React Navigation (or any other form of navigation) to be able to have multiple instances of the AppScreen open at once, without them being unmounted?此外,有没有办法使用 React Navigation(或任何其他形式的导航)来一次打开 AppScreen 的多个实例,而无需卸载它们? The functionality I'd like is similar to that of tabs in a browser, but since I'm rather new with React Native I'm unsure of what would be the right way to implement this.我想要的功能类似于浏览器中的选项卡,但由于我对 React Native 比较陌生,所以我不确定实现它的正确方法是什么。

Consider using react-native-navigation .考虑使用react-native-navigation The BottomTabs fit this use case perfectly. BottomTabs 非常适合这个用例。

Wix's library lets react-native open instances of itself on separate threads. Wix 的库允许 react-native 在单独的线程上打开自身的实例。 This makes packages like Webview not interfere with each other across the instances.这使得像 Webview 这样的包不会在实例之间相互干扰。

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

相关问题 当在react-navigation选项卡中的active选项卡上时,react-native-webview仅开始加载 - react-native-webview ONLY starts loading when on active tab in react-navigation tabNavigator react-native-webview 如何注入 javascript? - react-native-webview how to inject javascript? 使用react-navigation在react native中的不同导航器之间跳转 - Jumping between different navigators in react native using react-navigation 使用 react-native-webview 的 stopLoading 后 Webview 冻结 - Webview freezed after using the stopLoading of react-native-webview React-Native —如何使用react-navigation传递参数? - React-Native — How to pass parameters using react-navigation? React Native,React-Navigation - React Native, React-Navigation 即使遵循 react-navigation 文档,react-navigation 也无法在屏幕之间导航 - react-navigation can't navigate between screens even after following react-navigation docs 任务:react-native-webview:compileDebugJavaWithJavac 失败 - Task :react-native-webview:compileDebugJavaWithJavac FAILED 如何在不使用 navigation.navigate() 的情况下在 React Native 中的屏幕之间导航? - How can I navigate between screens in React Native without using navigation.navigate()? react-navigation如何从customDrawer导航导航 - react-navigation how to navigate from customDrawer navigation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM