0

In React Native, we have ImageBackground component, that will show the background image in a controller. I want to have same background image for all the controllers. Instead of writing it in all the controllers, is there a way whee I can set the background image for the entire application?

Satyam
  • 15,493
  • 31
  • 131
  • 244

1 Answers1

0

An easy approach is to define one MainComponent which is always the parent view of your screens. You can inject the subviews easily with {this.props.children}.

See simplified example below:

MainComponent :

class MainComponent extends Component {

   render() {

      return (
         <View style ...>
            <ImageBackground .../> 
          {this.props.children}
         </View>
      );
   } 
}

Controller:

import MainComponent from 'yourPATH'

class Controller extends Component {

   render() {
      return (
         <MainComponent>

          //ADD YOUR Regular Controller Screen here. 

         </MainComponent>
      );
   } 
}

You can read more about it here: https://reactjs.org/docs/composition-vs-inheritance.html

Tim
  • 10,459
  • 4
  • 36
  • 47
  • Thanks for the update. Even with this approach, I've to handle in all the controllers. – Satyam Mar 25 '20 at 15:16
  • @Satyam no thats not true, of course you could specify this MainComponent as a root view of your App. At some point you need a root view, that's for sure. – Tim Mar 25 '20 at 15:18