[英]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.