反应导航自定义tabBarComponent?

时间:2017-10-18 04:04:53

标签: reactjs react-native react-navigation

那样的navigationOptions代码。

static navigationOptions = ({navigation})=>({
        tabBarLabel:'查看',
        headerTitle:navigation.state.params.title,
        tabBarIcon: ({ tintColor,focused }) => (
            <Image style={SKIN.tabImage} source={focused?AppImages.MyPost.lookchoose:AppImages.MyPost.look}/>
        ),
    });

这是我的Tab组件,我怎样才能获得tabBarLabel和tabBarIcon?

export default class Tab extends Component {
    renderItem = (route, index) => {
        const {
            navigation,
            jumpToIndex,
        } = this.props;

        const focused = index === navigation.state.index;
        const color = focused ? this.props.activeTintColor : this.props.inactiveTintColor;
        return (
            <TouchableOpacity
                key={index}
                style={styles.tabItem}
                onPress={() => jumpToIndex(index)}
            >
                <View
                    style={styles.tabItem}>
                    {this.props.renderIcon(color,focused)}
                    <Text style={{ color }}>{this.props.getLabel()}</Text>
                </View>
            </TouchableOpacity>
        );
    };

    render(){
        console.log('Tab this.props',this.props);
        const {navigation,} = this.props;

        const {routes,} = navigation.state;
        return (
            <View style={styles.tab}>
                {routes && routes.map(this.renderItem)}
            </View>
        );
    }
}

我自定义Tab,现在我想用它,但有些bug告诉我。 像那样, imagebug 请帮帮我......

2 个答案:

答案 0 :(得分:0)

尝试使用以下代码更新render方法:

 render(){
            console.log('Tab this.props',this.props);
            const {navigation,} = this.props;

            const {routes,} = navigation.state;
            return (
                <View style={styles.tab}>
                    //pass down the route and the index to the renderItem function
                    {routes && routes.map((route,index) => this.renderItem(route, index) )}
                </View>
            );

答案 1 :(得分:0)

  renderItem = (route, index) => {
        const {
            navigation,
            jumpToIndex,
        } = this.props;

        const focused = index === navigation.state.index;
        const color = focused ? this.props.activeTintColor : this.props.inactiveTintColor;
        let TabScene = {
            focused:focused,
            route:route,
            tintColor:color
        };
        return (
            <TouchableOpacity
                key={route.key}
                style={styles.tabItem}
                onPress={() => jumpToIndex(index)}
            >
                <View
                    style={styles.tabItem}>
                    {this.props.renderIcon(TabScene)}
                    <Text style={{ color }}>{this.props.getLabel(TabScene)}</Text>
                </View>
            </TouchableOpacity>
        );
    };