如何动态更改react native应用程序中的标题标题文本?

时间:2017-08-20 14:52:41

标签: react-native

我一直在努力寻找听起来很简单的事情。当调用onpressed时我一直试图获得按钮。尝试了几个组合器,但没有任何作用。有什么想法吗?

export default class JobScreen extends React.Component {

    constructor(props) {
        super(props);
    }

    static navigationOptions = ({navigation}) => ({
        title: "Chat with"
    });

    onPressLearnMore(nav) {

        // how do I update the title header 

        console.log("On Pressed" + nav)
    }

    render() {
        const {navigate} = this.props.navigation;


        return (
            <View style={styles.container}>
                <Button
                    onPress={() => {this.onPressLearnMore(navigate)}}
                    title="Learn More"
                    color="#841584"
                />

            </View>
        );
    }
}

1 个答案:

答案 0 :(得分:3)

由于navigationOptions可以访问navigation对象,因此您可以使用this.props.navigation.setParam({ title: ‘some title’ })在当前屏幕上设置一些参数,然后在此navigationOptions中访问它

static navigationOptions = ({ navigation }) => {
  const { state: { params = {} } } = navigation;
  return {
    title: params.title || ‘default title’,
  };
}