2

I recently updated from RN 0.60 to 0.65, and after hours of debugging I can get my app to build and run again. However, for some reason the background colour of my app has changed from white to gray.

I'm using react-native-router-flux for navigation, but either the styling is broken in the latest react-native release, or I'm missing something obvious.

Here's how it has always been set up:

const RouterComponent = () => {
  const sceneConfig = {
    cardStyle: {
      backgroundColor: 'white',
    },
  };

  return (
    <Router {...sceneConfig}/>
    [...]
    </Router>

This no longer does anything. Here's what else I've tried:

  • Directly adding style properties to <Router> using sceneStyle, as recommended in the docs
  • Directly adding style properties to each individual scene by using style property

Neither of these approaches work, and I'm now stuck with an app that has a gray background (#f2f2f2) on every screen. I'm not even sure if this is an issue with react-native-router-flux but it definitely seems like the most likely cause.

Digging through the issues on the Github repo, I found one person flagging that this could be an incompatibility with react-native-screens, which seems to have been added to my project as a result of the upgrade to RN 0.65. This is a shot in the dark, as I'm not even sure what that library is used for.

Has anyone managed to change the background colour of their app on RN 0.65 and react-native-router-flux v4.3.0?

Edit:

Here's an example of how I tried to style individual scenes, which didn't work:

          <Scene
            title={'Profile'}
            renderTitle={() => <View />}
            // Neither of the below options has any effect
            sceneStyle={{backgroundColor: 'red'}}
            style={{backgroundColor: 'red'}}
            key="profile"
            hideNavBar
            icon={TabIcon}
            iconName="account-circle-outline"
            back={false}
            component={Profile}
          />
owiewio
  • 331
  • 3
  • 17
  • can you add the related code "Directly adding style properties to each individual scene by using style property" ? It should work properly. – nima Oct 31 '21 at 10:58
  • @nima Yes, I've updated the reply. I also tried using the `sceneStyle` property on the individual scene. Neither worked. – owiewio Oct 31 '21 at 22:50

1 Answers1

3

I suggest you create a wrapper component and wrap your pages on it to easily manage the specification.

this is the way I use it in my applications:

const MainView = ({children}) => {
  return (
    <View style={styles.container}>{children}</View>
  )
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: "#ff0",
  },
});

Now, use this component in other screens, for example in the HomeScreen:

const HomeScreen = () => {
  // rest of the codes ...

  return (
    <MainView>
      // rest of the codes ...
    </MainView>
  )
}

You can even pass the container style through props to the MainView instead of creating the style on it. in this way, you can pass different styles in different screens.

nima
  • 7,796
  • 12
  • 36
  • 53
  • 1
    Thanks for this suggestion. I tried it and it works, but it feels like quite a hacky fix to the problem. This is particularly messy when I have conditional rendering, as I need to wrap each case in the component. It works for now though, so thanks! – owiewio Nov 02 '21 at 10:16
  • I use this way for all my screens, even in web applications with `react.js` it gives you some advantage in the feature. @owiewio – nima Nov 02 '21 at 10:18
  • please don't forget to accept the answer if it really solved the issue. with best regards @owiewio – nima Nov 02 '21 at 10:18