-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathApp.js
64 lines (58 loc) · 1.56 KB
/
App.js
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import React from "react";
import { SafeAreaView, Text, View, Platform } from "react-native";
import StaticServer from "react-native-static-server";
import WebView from "react-native-webview";
import RNFS from "react-native-fs";
class App extends React.Component {
state = {
url: null
};
async componentDidMount() {
moveAndroidFiles();
let path = getPath();
this.server = new StaticServer(8080, path);
this.server.start().then(url => {
this.setState({ url });
});
}
componentWillUnmount() {
if (this.server && this.server.isRunning()) {
this.server.stop();
}
}
render() {
if (!this.state.url) {
return (
<SafeAreaView>
<Text>Hello World</Text>
</SafeAreaView>
);
}
return (
<SafeAreaView>
<Text>{this.state.url}</Text>
<View style={{ backgroundColor: "red", height: "100%", width: "100%" }}>
<WebView
style={{ flex: 1, marginBottom: 20 }}
source={{ uri: this.state.url }}
/>
</View>
</SafeAreaView>
);
}
}
function getPath() {
return Platform.OS === "android"
? RNFS.DocumentDirectoryPath + "/www"
: RNFS.MainBundlePath + "/www";
}
async function moveAndroidFiles() {
if (Platform.OS === "android") {
await RNFS.mkdir(RNFS.DocumentDirectoryPath + "/www");
const files = ["www/index.html", "www/index.css", "www/index.js"];
await files.forEach(async file => {
await RNFS.copyFileAssets(file, RNFS.DocumentDirectoryPath + "/" + file);
});
}
}
export default App;