-
Notifications
You must be signed in to change notification settings - Fork 1
/
WebViewContainer.tsx
35 lines (28 loc) · 1.15 KB
/
WebViewContainer.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import type { ComponentType } from 'react';
import { WebView, WebViewMessageEvent } from 'react-native-webview';
import { StackActions, NavigationProp, ParamListBase, RouteProp } from '@react-navigation/native';
const targetUrl = 'http://localhost:3000';
const WebviewContainer: ComponentType<{
route: RouteProp<ParamListBase, string> & { params?: Record<string, string> };
navigation: NavigationProp<ParamListBase>;
}> = ({ navigation, route }) => {
const url = route.params?.url ?? targetUrl;
const requestOnMessage = async (e: WebViewMessageEvent): Promise<void> => {
const nativeEvent = JSON.parse(e.nativeEvent.data);
if (nativeEvent?.type === 'ROUTER_EVENT') {
const path: string = nativeEvent.path;
if (path === 'back') {
const popAction = StackActions.pop(1);
navigation.dispatch(popAction);
} else {
const pushAction = StackActions.push('Details', {
url: path,
isStack: true,
});
navigation.dispatch(pushAction);
}
}
};
return <WebView originWhitelist={['*']} source={{ uri: url }} onMessage={requestOnMessage} />;
};
export default WebviewContainer;