Skip to content

Commit 48dcfa1

Browse files
Lulu Wufacebook-github-bot
Lulu Wu
authored andcommitted
Fix new arch example not render in RNTester (#39810)
Summary: Pull Request resolved: #39810 Two issues will be fixed: - Bridgeless has lazy view manager loading by default so the React Package that provides view managers must implement ViewManagerOnDemandReactPackage, we might could refactor the design of package classes later - ThemedReactContext should **NOT** be used directly to call function ```getJSModule```, since it doesn't overrides ```getJSModule``` for Bridgeless, we can use it's internal variable ```meactApplicationContext``` which should be an instance of BridgelessReactContext Reviewed By: cortinico Differential Revision: D49912656 fbshipit-source-id: a0bdd717612398e8d7a6f36d36dba241a3b06bd7
1 parent 9fee990 commit 48dcfa1

File tree

3 files changed

+23
-6
lines changed

3 files changed

+23
-6
lines changed

packages/rn-tester/android/app/src/main/java/com/facebook/react/uiapp/RNTesterReactHostDelegate.kt

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import com.facebook.react.JSEngineResolutionAlgorithm
1414
import com.facebook.react.ReactPackage
1515
import com.facebook.react.ReactPackageTurboModuleManagerDelegate
1616
import com.facebook.react.TurboReactPackage
17+
import com.facebook.react.ViewManagerOnDemandReactPackage
1718
import com.facebook.react.bridge.JSBundleLoader
1819
import com.facebook.react.bridge.NativeModule
1920
import com.facebook.react.bridge.ReactApplicationContext
@@ -108,7 +109,7 @@ class RNTesterReactHostDelegate internal constructor(context: Context) : ReactHo
108109
}
109110
}
110111
},
111-
object : ReactPackage {
112+
object : ViewManagerOnDemandReactPackage, ReactPackage {
112113
override fun createNativeModules(
113114
reactContext: ReactApplicationContext
114115
): List<NativeModule> = emptyList()
@@ -117,6 +118,22 @@ class RNTesterReactHostDelegate internal constructor(context: Context) : ReactHo
117118
reactContext: ReactApplicationContext
118119
): List<ViewManager<*, *>> =
119120
listOf(MyNativeViewManager(), MyLegacyViewManager(reactContext))
121+
122+
override fun getViewManagerNames(
123+
reactContext: ReactApplicationContext
124+
): Collection<String> =
125+
listOf(MyNativeViewManager.REACT_CLASS, MyLegacyViewManager.REACT_CLASS)
126+
127+
override fun createViewManager(
128+
reactContext: ReactApplicationContext,
129+
viewManagerName: String
130+
): ViewManager<*, *>? {
131+
return when (viewManagerName) {
132+
MyNativeViewManager.REACT_CLASS -> MyNativeViewManager()
133+
MyLegacyViewManager.REACT_CLASS -> MyLegacyViewManager(reactContext)
134+
else -> null
135+
}
136+
}
120137
})
121138
}
122139
}

packages/rn-tester/android/app/src/main/java/com/facebook/react/uiapp/component/MyLegacyViewManager.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ internal class MyLegacyViewManager(reactContext: ReactApplicationContext) :
2626

2727
override fun getName(): String = REACT_CLASS
2828

29-
override fun createViewInstance(reactContext: ThemedReactContext): MyNativeView =
30-
MyNativeView(reactContext).apply { setBackgroundColor(Color.RED) }
29+
override fun createViewInstance(themedReactContext: ThemedReactContext): MyNativeView =
30+
MyNativeView(themedReactContext).apply { setBackgroundColor(Color.RED) }
3131

3232
@ReactProp(name = ViewProps.OPACITY, defaultFloat = 1f)
3333
override fun setOpacity(view: MyNativeView, opacity: Float) {

packages/rn-tester/android/app/src/main/java/com/facebook/react/uiapp/component/MyNativeView.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,22 @@
77

88
package com.facebook.react.uiapp.component
99

10-
import android.content.Context
1110
import android.graphics.Color
1211
import android.graphics.drawable.GradientDrawable
1312
import android.view.View
1413
import com.facebook.react.bridge.Arguments
1514
import com.facebook.react.bridge.ReactContext
1615
import com.facebook.react.bridge.WritableArray
1716
import com.facebook.react.bridge.WritableMap
17+
import com.facebook.react.uimanager.ThemedReactContext
1818
import com.facebook.react.uimanager.UIManagerHelper
1919
import com.facebook.react.uimanager.events.Event
2020
import com.facebook.react.uimanager.events.RCTEventEmitter
2121

22-
class MyNativeView(context: Context) : View(context) {
22+
class MyNativeView(context: ThemedReactContext) : View(context) {
2323
private var currentColor = 0
2424
private var background: GradientDrawable = GradientDrawable()
25+
private var reactContext: ReactContext = context.getReactApplicationContext()
2526

2627
override fun setBackgroundColor(color: Int) {
2728
if (color != currentColor) {
@@ -51,7 +52,6 @@ class MyNativeView(context: Context) : View(context) {
5152
Color.colorToHSV(color, hsv)
5253
event.putMap("backgroundColor", backgroundColor)
5354

54-
val reactContext = context as ReactContext
5555
reactContext.getJSModule(RCTEventEmitter::class.java).receiveEvent(id, "onColorChanged", event)
5656
}
5757

0 commit comments

Comments
 (0)