更改 React Navigator 对象的背景颜色
Changing the background colour of a React Navigator object
我正在扩展 React.Component
以呈现 Navigator
:
<Navigator
renderScene={this.renderScene}
navigationBar={
<NavigationBar
routeMapper={NavigationBarRouteMapper}
style={styles.navBar} **// background colour is set here**
/>
/>
并沿导航器对象传递正在渲染的场景:
renderScene = (route, navigator) => {
if(route.maps) {
return <MapView navigator={navigator} />;
}
return <LoginScreen navigator={navigator} />;
}
和 MapView
看起来像这样:
type Props = {
navigator: Navigator;
}
class BTMapView extends React.Component {
props: Props;
constructor(props: Props) {
super(props);
...
}
}
既然我可以使用 this.props.navigator
引用导航器对象,我该如何使用它来通过命令式 API 调用覆盖其导航栏的背景颜色?
解决方案:
class BTNavigator extends React.Component {
constructor(props) {
super(props);
this.state = {
navBarStyle: styles.navBar,
};
}
render() {
return (
<Navigator
style={styles.container}
initialRoute={{}}
renderScene={this.renderScene}
navigationBar={
<NavigationBar
routeMapper={NavigationBarRouteMapper}
style={this.state.navBarStyle}
/>
}
/>
);
}
renderScene = (route, navigator) => {
...
// pass both navigator and a reference to this
return <LoginScreen navigator={navigator} btNavigator={this} />
}
setNavBarStyle(style) {
this.setState({
navBarStyle: style,
});
}
}
现在使用 navigator
和 btNavigator
:
type Props = {
navigator: Navigator,
btNavigator: BTNavigator,
};
class LoginScreen extends React.Component {
props: Props;
foo() {
this.props.btNavigator.setNavBarStyle({
backgroundColor: 'black',
});
this.props.navigator.push({
...
})
}
}
首先制作 NavigatorWrapper
class ,它将存储 Navigator
和一些额外的状态和方法,例如
setNavBarRed() {
this.setState({navBarColor: 'red'});
}
在 render()
方法中渲染 Navigator
就像你在上面写的一样,额外检查 NavigationBar
样式。只需在样式中设置 backgroundColor: this.state.navBarColor
。
最后,现在您可以使用道具了:this.props.navigatorWrapper.setNavBarRed()
请注意,如果您使用 react-redux
中的 connect
,您需要将 withRef
参数传递给 connect
调用。
我正在扩展 React.Component
以呈现 Navigator
:
<Navigator
renderScene={this.renderScene}
navigationBar={
<NavigationBar
routeMapper={NavigationBarRouteMapper}
style={styles.navBar} **// background colour is set here**
/>
/>
并沿导航器对象传递正在渲染的场景:
renderScene = (route, navigator) => {
if(route.maps) {
return <MapView navigator={navigator} />;
}
return <LoginScreen navigator={navigator} />;
}
和 MapView
看起来像这样:
type Props = {
navigator: Navigator;
}
class BTMapView extends React.Component {
props: Props;
constructor(props: Props) {
super(props);
...
}
}
既然我可以使用 this.props.navigator
引用导航器对象,我该如何使用它来通过命令式 API 调用覆盖其导航栏的背景颜色?
解决方案:
class BTNavigator extends React.Component {
constructor(props) {
super(props);
this.state = {
navBarStyle: styles.navBar,
};
}
render() {
return (
<Navigator
style={styles.container}
initialRoute={{}}
renderScene={this.renderScene}
navigationBar={
<NavigationBar
routeMapper={NavigationBarRouteMapper}
style={this.state.navBarStyle}
/>
}
/>
);
}
renderScene = (route, navigator) => {
...
// pass both navigator and a reference to this
return <LoginScreen navigator={navigator} btNavigator={this} />
}
setNavBarStyle(style) {
this.setState({
navBarStyle: style,
});
}
}
现在使用 navigator
和 btNavigator
:
type Props = {
navigator: Navigator,
btNavigator: BTNavigator,
};
class LoginScreen extends React.Component {
props: Props;
foo() {
this.props.btNavigator.setNavBarStyle({
backgroundColor: 'black',
});
this.props.navigator.push({
...
})
}
}
首先制作 NavigatorWrapper
class ,它将存储 Navigator
和一些额外的状态和方法,例如
setNavBarRed() {
this.setState({navBarColor: 'red'});
}
在 render()
方法中渲染 Navigator
就像你在上面写的一样,额外检查 NavigationBar
样式。只需在样式中设置 backgroundColor: this.state.navBarColor
。
最后,现在您可以使用道具了:this.props.navigatorWrapper.setNavBarRed()
请注意,如果您使用 react-redux
中的 connect
,您需要将 withRef
参数传递给 connect
调用。