无法将道具传递给bottomTabNavigation

时间:2019-07-22 18:23:31

标签: javascript reactjs react-native

我正在尝试将用户信息传递给createBottomTabNavigator,然后将ProfileStack上的标题设置为用户名

我尝试通过this.props.navigation.setParams(...),但是由于创建底部标签导航器的代码如下所示,我无法做到这一点

ProfileStack.navigationOptions = {
    tabBarLabel: () => {
        return null
    },
    tabBarIcon: ({focused}) => (
        <Image style={[...styles]}
               source={uri}/>
    ),
};

const tabNavigator = createBottomTabNavigator({
    HomeStack,
    SearchStack,
    DashboardStack,
    ProfileStack,
});
import React, {Component} from 'react';
import {StyleSheet, SafeAreaView, View, Text} from 'react-native';

export default class SettingsScreen extends Component {

    static navigationOptions = {
        title: 'Profil',
    }

    render() {
        return (
            <SafeAreaView>
                <Text>Profil</Text>
            </SafeAreaView>
        )
    }
}

我希望将标题更改为通过道具传递的内容, 喜欢

let name = this.props.navigation.props.getParam('user').name

static navigationOptions = {
    title: name
}

1 个答案:

答案 0 :(得分:0)

您不能像在代码中那样将信息传递给静态navigationOptions,您可以尝试改为在导航中传递新标题来设置参数

static navigationOptions ({navigation}) => {
    return {
         title: navigation.state.params.title
    }
}

// when some events are triggered
this.props.navigation.setParams({title: "Example"});