Skip to content

Commit 68c0114

Browse files
dazakdevclaude
andcommitted
Fix non-compiling Java snippet in Android Fabric guide
The ReactWebView.java snippet fails to compile as written: LayoutParams resolves to AbsoluteLayout.LayoutParams which has no (int, int) constructor, there is no context variable in scope, and EventDispatcher is never imported. Also enable JavaScript, which the Android WebView disables by default while the WKWebView used on iOS enables it, so that the component behaves the same on both platforms. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
1 parent bcb0f3a commit 68c0114

1 file changed

Lines changed: 8 additions & 2 deletions

File tree

docs/fabric-native-components-android.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ package com.webview;
3838

3939
import android.content.Context;
4040
import android.util.AttributeSet;
41+
import android.view.ViewGroup;
4142
import android.webkit.WebView;
4243
import android.webkit.WebViewClient;
4344

@@ -46,6 +47,7 @@ import com.facebook.react.bridge.WritableMap;
4647
import com.facebook.react.bridge.ReactContext;
4748
import com.facebook.react.uimanager.UIManagerHelper;
4849
import com.facebook.react.uimanager.events.Event;
50+
import com.facebook.react.uimanager.events.EventDispatcher;
4951

5052
public class ReactWebView extends WebView {
5153
public ReactWebView(Context context) {
@@ -64,7 +66,8 @@ public class ReactWebView extends WebView {
6466
}
6567

6668
private void configureComponent() {
67-
this.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
69+
this.getSettings().setJavaScriptEnabled(true);
70+
this.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
6871
this.setWebViewClient(new WebViewClient() {
6972
@Override
7073
public void onPageFinished(WebView view, String url) {
@@ -74,7 +77,7 @@ public class ReactWebView extends WebView {
7477
}
7578

7679
public void emitOnScriptLoaded(OnScriptLoadedEventResult result) {
77-
ReactContext reactContext = (ReactContext) context;
80+
ReactContext reactContext = (ReactContext) getContext();
7881
int surfaceId = UIManagerHelper.getSurfaceId(reactContext);
7982
EventDispatcher eventDispatcher = UIManagerHelper.getEventDispatcherForReactTag(reactContext, getId());
8083
WritableMap payload = Arguments.createMap();
@@ -142,6 +145,7 @@ class ReactWebView: WebView {
142145
}
143146

144147
private fun configureComponent() {
148+
this.settings.javaScriptEnabled = true
145149
this.layoutParams = LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)
146150
this.webViewClient = object : WebViewClient() {
147151
override fun onPageFinished(view: WebView, url: String) {
@@ -187,6 +191,8 @@ The `ReactWebView` extends the Android `WebView` so you can reuse all the proper
187191

188192
The class defines the three Android constructors but defers their actual implementation to the private `configureComponent` function. This function takes care of initializing all the components specific properties: in this case you are setting the layout of the `WebView` and you are defining the `WebClient` that you use to customize the behavior of the `WebView`. In this code, the `ReactWebView` emits an event when the page finishes loading, by implementing the `WebClient`'s `onPageFinished` method.
189193

194+
`configureComponent` also enables JavaScript. The Android `WebView` [disables it by default](<https://developer.android.com/reference/android/webkit/WebSettings#setJavaScriptEnabled(boolean)>), while the `WKWebView` used on iOS enables it. Without this line, the same component would run web content differently on the two platforms. Only enable JavaScript for content you trust.
195+
190196
The code then defines a helper function to actually emit an event. To emit an event, you have to:
191197

192198
- grab a reference to the `ReactContext`;

0 commit comments

Comments
 (0)