简体   繁体   中英

React Native navigation.navigate not working on headerRight button

I face an issue when I click on header right button on navigation bar. I need to move next screen from headerRight button but I am getting 'undefined is not an object (evaluating _this4.props. navigation)' .

Here is my code please check me and help me.

static navigationOptions = ({navigation}) => ({
        headerRight: (
            <TouchableOpacity onPress={() => this.props.navigation.navigate('Setting')}
                style={{right: Platform.OS === 'ios' ? 
                       Dimensions.get("window").height < 667 ?  '10%' : '30%' : '25%', 
                       backgroundColor: 'transparent', 
                       paddingLeft: 15}}>
                <Image style={{width: 20, height: 20}} 
                       source={require('../assets/icons/setting/setting.png')}/>
            </TouchableOpacity>
        ),
});

You are passing navigation as a prop in navigationOptions , so that will be available inside as navigation directly not with this.props.navigation .

static navigationOptions = ({navigation}) => ({
        headerRight: (
            <TouchableOpacity onPress={() => navigation.navigate('Setting')}
                style={{right: Platform.OS === 'ios' ? 
                       Dimensions.get("window").height < 667 ?  '10%' : '30%' : '25%', 
                       backgroundColor: 'transparent', 
                       paddingLeft: 15}}>
                <Image style={{width: 20, height: 20}} 
                       source={require('../assets/icons/setting/setting.png')}/>
            </TouchableOpacity>
        ),
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM