Skip to content

Commit 34aa7c0

Browse files
authored
Merge pull request #37 from inokawa/expo
Fix bug that babelTransformer do not work correctly in expo
2 parents ffeb3f4 + c6a3544 commit 34aa7c0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+11413
-5
lines changed

README.md

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ npm install preact
5858

5959
## Usage
6060

61-
1. Fix `metro.config.js` to use babelTransformer from this library.
61+
### 1. Fix `metro.config.js` to use babelTransformer from this library.
62+
63+
#### React Native
6264

6365
```javascript
6466
module.exports = {
@@ -77,7 +79,25 @@ module.exports = {
7779
};
7880
```
7981

80-
2. Make entry file for web app.
82+
#### Expo
83+
84+
```javascript
85+
const { getDefaultConfig } = require("expo/metro-config");
86+
87+
const config = getDefaultConfig(__dirname);
88+
89+
module.exports = {
90+
...config,
91+
transformer: {
92+
...config.transformer,
93+
babelTransformerPath: require.resolve(
94+
"react-native-react-bridge/lib/plugin"
95+
),
96+
},
97+
};
98+
```
99+
100+
### 2. Make entry file for web app.
81101

82102
- If you use React in web, import modules from `react` and `react-native-react-bridge/lib/web`.
83103
- If you use Preact in web, import modules from `preact` and `react-native-react-bridge/lib/web/preact`.
@@ -126,7 +146,7 @@ const Root = () => {
126146
export default webViewRender(<Root />);
127147
```
128148

129-
3. Use the entry file in your React Native app with WebView.
149+
### 3. Use the entry file in your React Native app with WebView.
130150

131151
```jsx
132152
// App.js
@@ -160,12 +180,14 @@ const App = () => {
160180
};
161181
```
162182

163-
4. Start your React Native app!
183+
### 4. Start your React Native app!
164184

165185
## Demo
166186

167187
This repository includes demo app.
168188

189+
### React Native
190+
169191
Before running this app, please prepare environment for React Native (https://reactnative.dev/docs/environment-setup).
170192

171193
```sh
@@ -174,3 +196,12 @@ cd examples/DemoApp
174196
npm install
175197
npm run ios # or npm run android
176198
```
199+
200+
### Expo
201+
202+
```sh
203+
git clone [email protected]:inokawa/react-native-react-bridge.git
204+
cd examples/DemoAppExpo
205+
yarn
206+
expo start
207+
```

examples/DemoAppExpo/.buckconfig

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
[android]
3+
target = Google Inc.:Google APIs:23
4+
5+
[maven_repositories]
6+
central = https://repo1.maven.org/maven2
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.pbxproj -text

examples/DemoAppExpo/.gitignore

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# OSX
2+
#
3+
.DS_Store
4+
5+
# Xcode
6+
#
7+
build/
8+
*.pbxuser
9+
!default.pbxuser
10+
*.mode1v3
11+
!default.mode1v3
12+
*.mode2v3
13+
!default.mode2v3
14+
*.perspectivev3
15+
!default.perspectivev3
16+
xcuserdata
17+
*.xccheckout
18+
*.moved-aside
19+
DerivedData
20+
*.hmap
21+
*.ipa
22+
*.xcuserstate
23+
project.xcworkspace
24+
25+
# Android/IntelliJ
26+
#
27+
build/
28+
.idea
29+
.gradle
30+
local.properties
31+
*.iml
32+
*.hprof
33+
34+
# node.js
35+
#
36+
node_modules/
37+
npm-debug.log
38+
yarn-error.log
39+
40+
# BUCK
41+
buck-out/
42+
\.buckd/
43+
*.keystore
44+
!debug.keystore
45+
46+
# Bundle artifacts
47+
*.jsbundle
48+
49+
# CocoaPods
50+
/ios/Pods/
51+
52+
# Expo
53+
.expo/
54+
web-build/

examples/DemoAppExpo/App.tsx

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import React, { useState } from "react";
2+
import {
3+
SafeAreaView,
4+
StyleSheet,
5+
Text,
6+
View,
7+
TextInput,
8+
Pressable,
9+
} from "react-native";
10+
import WebView from "react-native-webview";
11+
import { useBridge } from "react-native-react-bridge";
12+
import webApp from "./WebApp";
13+
14+
export default function App() {
15+
const [data, setData] = useState("This is React Native");
16+
const { ref, source, onMessage, emit } = useBridge<string>(
17+
webApp,
18+
(message) => {
19+
if (message.type === "hi") {
20+
setData(message.data);
21+
}
22+
}
23+
);
24+
25+
return (
26+
<SafeAreaView style={styles.container}>
27+
<View style={styles.top}>
28+
<WebView ref={ref} source={source} onMessage={onMessage} />
29+
</View>
30+
<View style={styles.bottom}>
31+
<TextInput
32+
style={styles.input}
33+
onChangeText={(text) => setData(text)}
34+
value={data}
35+
/>
36+
<Pressable
37+
onPress={() => emit({ type: "hello", data: data })}
38+
style={styles.button}
39+
>
40+
<Text>send to Web</Text>
41+
</Pressable>
42+
</View>
43+
</SafeAreaView>
44+
);
45+
}
46+
47+
const styles = StyleSheet.create({
48+
container: {
49+
flex: 1,
50+
},
51+
top: {
52+
flex: 1,
53+
borderWidth: 4,
54+
borderColor: "gray",
55+
},
56+
bottom: {
57+
padding: 4,
58+
flex: 1,
59+
},
60+
input: {
61+
height: 40,
62+
borderColor: "gray",
63+
borderWidth: 1,
64+
},
65+
button: {
66+
borderRadius: 10,
67+
padding: 8,
68+
backgroundColor: "lightgray",
69+
},
70+
});

examples/DemoAppExpo/WebApp.tsx

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import React, { useState } from "react";
2+
import {
3+
webViewRender,
4+
emit,
5+
useSubscribe,
6+
} from "react-native-react-bridge/lib/web";
7+
import "./example.css";
8+
9+
const style = {
10+
width: "100vw",
11+
height: "100vh",
12+
margin: "auto",
13+
backgroundColor: "lightblue",
14+
};
15+
16+
const Root = () => {
17+
const [data, setData] = useState("This is Web");
18+
useSubscribe<string>((message) => {
19+
if (message.type === "hello") {
20+
setData(message.data);
21+
}
22+
});
23+
return (
24+
<div style={style}>
25+
<textarea value={data} onChange={(e) => setData(e.target.value)} />
26+
<div>
27+
<button onClick={() => emit({ type: "hi", data: data })}>
28+
send to React Native
29+
</button>
30+
</div>
31+
</div>
32+
);
33+
};
34+
35+
export default webViewRender(<Root />);
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import 'react-native';
2+
import React from 'react';
3+
import App from '../App.tsx';
4+
5+
// Note: test renderer must be required after react-native.
6+
import renderer from 'react-test-renderer';
7+
8+
it('renders correctly', () => {
9+
renderer.create(<App />);
10+
});
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# To learn about Buck see [Docs](https://buckbuild.com/).
2+
# To run your application with Buck:
3+
# - install Buck
4+
# - `npm start` - to start the packager
5+
# - `cd android`
6+
# - `keytool -genkey -v -keystore keystores/debug.keystore -storepass android -alias androiddebugkey -keypass android -dname "CN=Android Debug,O=Android,C=US"`
7+
# - `./gradlew :app:copyDownloadableDepsToLibs` - make all Gradle compile dependencies available to Buck
8+
# - `buck install -r android/app` - compile, install and run application
9+
#
10+
11+
load(":build_defs.bzl", "create_aar_targets", "create_jar_targets")
12+
13+
lib_deps = []
14+
15+
create_aar_targets(glob(["libs/*.aar"]))
16+
17+
create_jar_targets(glob(["libs/*.jar"]))
18+
19+
android_library(
20+
name = "all-libs",
21+
exported_deps = lib_deps,
22+
)
23+
24+
android_library(
25+
name = "app-code",
26+
srcs = glob([
27+
"src/main/java/**/*.java",
28+
]),
29+
deps = [
30+
":all-libs",
31+
":build_config",
32+
":res",
33+
],
34+
)
35+
36+
android_build_config(
37+
name = "build_config",
38+
package = "com.demoappexpo",
39+
)
40+
41+
android_resource(
42+
name = "res",
43+
package = "com.demoappexpo",
44+
res = "src/main/res",
45+
)
46+
47+
android_binary(
48+
name = "app",
49+
keystore = "//android/keystores:debug",
50+
manifest = "src/main/AndroidManifest.xml",
51+
package_type = "debug",
52+
deps = [
53+
":app-code",
54+
],
55+
)

0 commit comments

Comments
 (0)