Skip to content

Commit

Permalink
Fix new arch example not render in RNTester (#39810)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Lulu Wu authored and facebook-github-bot committed Oct 5, 2023
1 parent 9fee990 commit 48dcfa1
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import com.facebook.react.JSEngineResolutionAlgorithm
import com.facebook.react.ReactPackage
import com.facebook.react.ReactPackageTurboModuleManagerDelegate
import com.facebook.react.TurboReactPackage
import com.facebook.react.ViewManagerOnDemandReactPackage
import com.facebook.react.bridge.JSBundleLoader
import com.facebook.react.bridge.NativeModule
import com.facebook.react.bridge.ReactApplicationContext
Expand Down Expand Up @@ -108,7 +109,7 @@ class RNTesterReactHostDelegate internal constructor(context: Context) : ReactHo
}
}
},
object : ReactPackage {
object : ViewManagerOnDemandReactPackage, ReactPackage {
override fun createNativeModules(
reactContext: ReactApplicationContext
): List<NativeModule> = emptyList()
Expand All @@ -117,6 +118,22 @@ class RNTesterReactHostDelegate internal constructor(context: Context) : ReactHo
reactContext: ReactApplicationContext
): List<ViewManager<*, *>> =
listOf(MyNativeViewManager(), MyLegacyViewManager(reactContext))

override fun getViewManagerNames(
reactContext: ReactApplicationContext
): Collection<String> =
listOf(MyNativeViewManager.REACT_CLASS, MyLegacyViewManager.REACT_CLASS)

override fun createViewManager(
reactContext: ReactApplicationContext,
viewManagerName: String
): ViewManager<*, *>? {
return when (viewManagerName) {
MyNativeViewManager.REACT_CLASS -> MyNativeViewManager()
MyLegacyViewManager.REACT_CLASS -> MyLegacyViewManager(reactContext)
else -> null
}
}
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ internal class MyLegacyViewManager(reactContext: ReactApplicationContext) :

override fun getName(): String = REACT_CLASS

override fun createViewInstance(reactContext: ThemedReactContext): MyNativeView =
MyNativeView(reactContext).apply { setBackgroundColor(Color.RED) }
override fun createViewInstance(themedReactContext: ThemedReactContext): MyNativeView =
MyNativeView(themedReactContext).apply { setBackgroundColor(Color.RED) }

@ReactProp(name = ViewProps.OPACITY, defaultFloat = 1f)
override fun setOpacity(view: MyNativeView, opacity: Float) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,22 @@

package com.facebook.react.uiapp.component

import android.content.Context
import android.graphics.Color
import android.graphics.drawable.GradientDrawable
import android.view.View
import com.facebook.react.bridge.Arguments
import com.facebook.react.bridge.ReactContext
import com.facebook.react.bridge.WritableArray
import com.facebook.react.bridge.WritableMap
import com.facebook.react.uimanager.ThemedReactContext
import com.facebook.react.uimanager.UIManagerHelper
import com.facebook.react.uimanager.events.Event
import com.facebook.react.uimanager.events.RCTEventEmitter

class MyNativeView(context: Context) : View(context) {
class MyNativeView(context: ThemedReactContext) : View(context) {
private var currentColor = 0
private var background: GradientDrawable = GradientDrawable()
private var reactContext: ReactContext = context.getReactApplicationContext()

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

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

Expand Down

0 comments on commit 48dcfa1

Please sign in to comment.