简体   繁体   English

ReactNative 中整个应用程序的背景图像

[英]Background image for entire application in ReactNative

In React Native, we have ImageBackground component, that will show the background image in a controller.在 React Native 中,我们有 ImageBackground 组件,它将在控制器中显示背景图像。 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?不是在所有控制器中都写它,有没有办法为整个应用程序设置背景图像?

An easy approach is to define one MainComponent which is always the parent view of your screens.一种简单的方法是定义一个 MainComponent,它始终是屏幕的父视图。 You can inject the subviews easily with {this.props.children} .您可以使用{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您可以在此处阅读更多相关信息: https : //reactjs.org/docs/composition-vs-inheritance.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM