react-native - React Native - 动态更改 tabnavigator 中的背景颜色

标签 react-native tabnavigator stack-navigator

我想根据我的 API 响应动态更改我的标签导航器背景颜色,所以下面是我的代码

_TabNavigator.js

const MyTabnav = TabNavigator({
ScreenOne: {
    screen: ({ navigation, screenProps }) => <ScreenOneNav screenProps={{ tabbarNavigation: navigation, ...screenProps }} onNavigationStateChange={null} />,
    navigationOptions: {
        tabBarLabel: 'ScreenOne',
        tabBarIcon: ({ tintColor }) => (
            <View style={[styles.tabViewBox]}>
                 <Text style={[styles.tabText, { color: tintColor }]}>ScreenOne</Text>
            </View>
        )
    }
},
ScreenTwo: {
    screen: ({ navigation, screenProps }) => <ScreenOneNav screenProps={{ tabbarNavigation: navigation, ...screenProps, }} onNavigationStateChange={null} />,
    navigationOptions: {
        tabBarLabel: 'ScreenOne',
        tabBarIcon: ({ tintColor }) => (
            <View style={[styles.tabViewBox]}>
                <Text style={[styles.tabText, { color: tintColor }]}>ScreenTwo</Text>
            </View>
        )
    }
},
ScreenThree: {
    screen: ({ navigation, screenProps }) => <StockNotificationNav screenProps={{ tabbarNavigation: navigation, ...screenProps }} onNavigationStateChange={null} />,
    navigationOptions: {
        tabBarLabel: 'Notifications',
        tabBarIcon: ({ tintColor }) => (
            <View style={[styles.tabViewBox]}>
                 <Text style={[styles.tabText, { color: tintColor }]}>ScreenThree</Text>
            </View>
        )
    }
},
},
 {

    tabBarOptions: {

        style: {
            backgroundColor: white,
            height: 55,
            borderTopColor: 'transparent',
            borderTopWidth: 1,
            paddingRight: 10,
            paddingLeft: 10,
            borderTopWidth: 1,
            borderTopColor: grayPlaceHolder
        },
        showLabel: false,
        showIcon : true,
    },
    tabBarComponent : TabBarBottom,

    initialRouteName: 'ScreenTwo',
    tabBarPosition: 'bottom',
    animationEnabled: false,
    swipeEnabled: false
}, []);


var styles = StyleSheet.create({
tabText: {
    fontSize: 10,
    fontWeight: "600",
    flex: 4,
},
tabViewBox: {
    flex: 1,
    alignItems: "center",
},
 tabIcon: {
    flex: 5,
    alignSelf: "center",
    marginTop: 10
  },
});
export default StocksTabNav;

我想在我的 ScreenTwo.js 文件中更改我的 tabnavigtor 背景颜色,其中包含 API 响应代码,因为它 tabnavigator 背景颜色 (backgroundColor) 应该根据 API 更改为黑色或白色响应所以我怎么能做到这一点?你的所有建议都很有值(value)

根据 Rahul 建议更新代码后给出以下警告消息

enter image description here

最佳答案

您可以做的是制作一个自定义 tabBar 组件并使用 javaScript 不变性概念,您可以覆盖 tabBarOptions 的样式。

     const MyTabnav = TabNavigator({ScreenOne: {
        screen: ({ navigation, screenProps }) => <ScreenOneNav screenProps={{ tabbarNavigation: navigation, ...screenProps }} onNavigationStateChange={null} />,
        navigationOptions: {
            tabBarLabel: 'ScreenOne',
            tabBarIcon: ({ tintColor }) => (
                <View style={[styles.tabViewBox]}>
                     <Text style={[styles.tabText, { color: tintColor }]}>ScreenOne</Text>
                </View>
            )
        }
    },
    ScreenTwo: {
        screen: ({ navigation, screenProps }) => <ScreenOneNav screenProps={{ tabbarNavigation: navigation, ...screenProps, }} onNavigationStateChange={null} />,
        navigationOptions: {
            tabBarLabel: 'ScreenOne',
            tabBarIcon: ({ tintColor }) => (
                <View style={[styles.tabViewBox]}>
                    <Text style={[styles.tabText, { color: tintColor }]}>ScreenTwo</Text>
                </View>
            )
        }
    },
    ScreenThree: {
        screen: ({ navigation, screenProps }) => <StockNotificationNav screenProps={{ tabbarNavigation: navigation, ...screenProps }} onNavigationStateChange={null} />,
        navigationOptions: {
            tabBarLabel: 'Notifications',
            tabBarIcon: ({ tintColor }) => (
                <View style={[styles.tabViewBox]}>
                     <Text style={[styles.tabText, { color: tintColor }]}>ScreenThree</Text>
                </View>
            )
        }
    },
    },
     {

        tabBarOptions: {

            style: {
                backgroundColor: white,
                height: 55,
                borderTopColor: 'transparent',
                borderTopWidth: 1,
                paddingRight: 10,
                paddingLeft: 10,
                borderTopWidth: 1,
                borderTopColor: grayPlaceHolder
            },
            showLabel: false,


         showIcon : true,
        },

//Here Goes Your CustomTabBar Component 
        tabBarComponent : CustomTabBarComponent,
        initialRouteName: 'ScreenTwo',
        tabBarPosition: 'bottom',
        animationEnabled: false,
        swipeEnabled: false
    }, []);

CustomTabBarComponent.js

     const TabBar = (props) => {
          const { navigationState } = props;
          let newProps = props;

            newProps = Object.assign(
              {},
              props,
              {
                style: {

         // get value from redux store and set it here 
                  backgroundColor: 'rgba(0,0,0,0.1)',
                  position: 'absolute',
                  bottom: 0,
                  left: 0,
                  right: 0
                },
                activeTintColor: '#fff',
                inactiveTintColor: '#bbb',
              },
            );


          return <TabBarBottom {...newProps} />;
        };

现在您可以将此 CustomTabBarComponent 与 Redux 存储连接起来,并可以更改您想要的任何属性的值。

关于react-native - React Native - 动态更改 tabnavigator 中的背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49026608/

相关文章:

reactjs - 在react-native中发送请求 header /授权 axios.post

android - 无法使用android模拟器打开React Native项目

reactjs - 在 TabNavigator 中隐藏标签 - ReactNavigation

javascript - 如何将 mapStateToProps/redux 添加到 react-navigation TabNavigator?

ios - React Native:iOS中的透明Stack Navigator不起作用

reactjs - 字段 : android - should NOT have additional property 'enableDangerousExperimentalLeanBuilds'

ios - 如何修复不为 iOS 构建的 React Native App

javascript - React Navigation 默认背景色

react-native - React Navigation 将参数传递给嵌套的 stackNavigator