如何在React Native应用程序中动态更改标题标题文本?

tec*_*rch 3 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>
        );
    }
}
Run Code Online (Sandbox Code Playgroud)

hyb*_*175 5

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

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