Skip to content

Commit

Permalink
fix(Android): Set stateWrapper in ScreenViewManager in Fabric (#1944)
Browse files Browse the repository at this point in the history
## Description

We are in the process of migrating to New Architecture. We noticed a
suspicious error message in LogCat, and discovered that
`FabricEnabledViewGroup`'s state updates in `updateScreenSizeFabric()`
would fail with "setState called without a StateWrapper", due to:


https://github.com/facebook/react-native/blob/79d620dff7561e6c9370184f9f19e79643486716/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/FabricViewStateManager.java#L65

## Changes

Call `fabricViewStateManager.setStateWrapper()` in
`ScreenViewManager.kt`.

<img width="1285" alt="Screen Shot 2023-10-23 at 11 20 09 AM"
src="https://github.com/software-mansion/react-native-screens/assets/12057449/3411e42d-fba3-4b11-b601-66cfb557cc62">

<img width="1808" alt="Screen Shot 2023-10-23 at 11 32 28 AM"
src="https://github.com/software-mansion/react-native-screens/assets/12057449/28d1f518-da4a-4971-bfb2-5c2c5b7458e0">

## Test code and steps to reproduce

Run `FabricExample` and `Example`. View LogCat logs.

## Checklist

- [ ] Included code example that can be used to test this change
- [ ] Updated TS types
- [ ] Updated documentation: <!-- For adding new props to native-stack
-->
- [ ]
https://github.com/software-mansion/react-native-screens/blob/main/guides/GUIDE_FOR_LIBRARY_AUTHORS.md
- [ ]
https://github.com/software-mansion/react-native-screens/blob/main/native-stack/README.md
- [ ]
https://github.com/software-mansion/react-native-screens/blob/main/src/types.tsx
- [ ]
https://github.com/software-mansion/react-native-screens/blob/main/src/native-stack/types.tsx
- [ ] Ensured that CI passes

---------

Co-authored-by: Tymoteusz Boba <Tymoteusz.Boba@gmail.com>
  • Loading branch information
joemun and tboba authored Oct 27, 2023
1 parent b1725c6 commit 5eb15d8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
14 changes: 14 additions & 0 deletions android/src/main/java/com/swmansion/rnscreens/ScreenViewManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import com.facebook.react.bridge.JSApplicationIllegalArgumentException
import com.facebook.react.bridge.ReadableMap
import com.facebook.react.common.MapBuilder
import com.facebook.react.module.annotations.ReactModule
import com.facebook.react.uimanager.ReactStylesDiffMap
import com.facebook.react.uimanager.StateWrapper
import com.facebook.react.uimanager.ThemedReactContext
import com.facebook.react.uimanager.ViewGroupManager
import com.facebook.react.uimanager.ViewManagerDelegate
Expand Down Expand Up @@ -35,6 +37,18 @@ class ScreenViewManager : ViewGroupManager<Screen>(), RNSScreenManagerInterface<
setActivityState(view, activityState.toInt())
}

override fun updateState(
view: Screen,
props: ReactStylesDiffMap?,
stateWrapper: StateWrapper?
): Any? {
if (BuildConfig.IS_NEW_ARCHITECTURE_ENABLED) {
// fabricViewStateManager should never be null in Fabric. The null check is only for Paper's empty impl.
view.fabricViewStateManager?.setStateWrapper(stateWrapper)
}
return super.updateState(view, props, stateWrapper)
}

@ReactProp(name = "activityState")
fun setActivityState(view: Screen, activityState: Int) {
if (activityState == -1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@ package com.swmansion.rnscreens

import android.view.ViewGroup
import com.facebook.react.bridge.ReactContext
import com.facebook.react.uimanager.FabricViewStateManager

abstract class FabricEnabledViewGroup constructor(context: ReactContext?) : ViewGroup(context) {

val fabricViewStateManager get() = null as FabricViewStateManager?

protected fun updateScreenSizeFabric(width: Int, height: Int) {
// do nothing
}
Expand Down

0 comments on commit 5eb15d8

Please sign in to comment.