【发布时间】:2020-03-24 15:09:29
【问题描述】:
在 React Native 中,我们有 ImageBackground 组件,它将在控制器中显示背景图像。我想为所有控制器使用相同的背景图像。除了在所有控制器中编写它,有没有办法可以为整个应用程序设置背景图像?
【问题讨论】:
标签: react-native imagebackground
在 React Native 中,我们有 ImageBackground 组件,它将在控制器中显示背景图像。我想为所有控制器使用相同的背景图像。除了在所有控制器中编写它,有没有办法可以为整个应用程序设置背景图像?
【问题讨论】:
标签: react-native imagebackground
一种简单的方法是定义一个 MainComponent,它始终是屏幕的父视图。您可以使用{this.props.children} 轻松注入子视图。
参见下面的简化示例:
主组件:
class MainComponent extends Component {
render() {
return (
<View style ...>
<ImageBackground .../>
{this.props.children}
</View>
);
}
}
控制器:
import MainComponent from 'yourPATH'
class Controller extends Component {
render() {
return (
<MainComponent>
//ADD YOUR Regular Controller Screen here.
</MainComponent>
);
}
}
您可以在此处阅读更多信息:https://reactjs.org/docs/composition-vs-inheritance.html
【讨论】: