Skip to content

Commit

Permalink
fix(Android): Set stateWrapper in ScreenViewManager in Fabric (softwa…
Browse files Browse the repository at this point in the history
…re-mansion#1944)

## 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 <[email protected]>
  • Loading branch information
2 people authored and ja1ns committed Oct 9, 2024
1 parent 527fd8c commit 9729f74
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 9729f74

Please sign in to comment.