undefined is not an object (evaluating '_this.

2019-08-27 01:43发布

This was working fine within the Login.js component:

<View style={{flexDirection: 'row', justifyContent:"center"}}>
                        <TouchableHighlight onPress={() => this.onPressSocialButton('tel')}  >
                            <Image source={require('./img/icono-tel.png')} style={{width:70, height:70,margin:10}} />                            
                        </TouchableHighlight>                    
                        <TouchableHighlight onPress={() => this.onPressSocialButton('wa')}>
                            <Image source={require('./img/icono-whatsapp.png')} style={{width:70, height:70,margin:10}} />
                        </TouchableHighlight>
                        <TouchableHighlight onPress={() => this.onPressSocialButton('fb')}>
                            <Image source={require('./img/icono-like.png')} style={{width:70, height:70,margin:10}} />
                        </TouchableHighlight>
                        <TouchableHighlight onPress={() => this.onPressSocialButton('ingrm')}>
                            <Image source={require('./img/icono-like-instagram.png')} style={{width:70, height:70,margin:10}} />
                        </TouchableHighlight>
</View>

    onPressSocialButton = (media) => {
            if (media === 'tel') {
                this.props.navigation.navigate('TelUtiles');
            } else if (media === 'wa') {
                Linking.openURL('whatsapp://send?text==%C2%A1Hola!%20Quiero%20realizar%20una%20consulta.&phone=5493416931539').catch(err => console.error('An error occurred', err));                
            } else if (media === 'fb') {
                Linking.openURL('https://www.facebook.com/n/?mascotaweb');                    
            } else if (media === 'ingrm') {
                Linking.openURL('http://instagram.com/_u/mascotaweb');
            }
        };

But when i move this to a separated component i receive the error in the title of this post. SocialFooter.js:

import React, { Component } from 'react';
import {
    View,
    Image,    
    TouchableHighlight,
    Linking
} from 'react-native';

export default class SocialFooter extends Component {

    static navigationOptions = { header: null }

    constructor(props) {
        super(props);                
    }

    onPressSocialButton = (media) => {
        if (media === 'tel') {
            this.props.navigation.navigate('TelUtiles');
        } else if (media === 'wa') {
            Linking.openURL('whatsapp://send?text==%C2%A1Hola!%20Quiero%20realizar%20una%20consulta.&phone=5493416931539').catch(err => console.error('An error occurred', err));                
        } else if (media === 'fb') {
            Linking.openURL('https://www.facebook.com/n/?mascotaweb');                    
        } else if (media === 'ingrm') {
            Linking.openURL('http://instagram.com/_u/mascotaweb');
        }
    };

    render() {
        return (
            <View style={{flexDirection: 'row', justifyContent:"center"}}>
                <TouchableHighlight onPress={() => this.onPressSocialButton('tel')}  >
                    <Image source={require('./img/icono-tel.png')} style={{width:70, height:70,margin:10}} />                            
                </TouchableHighlight>                    
                <TouchableHighlight onPress={() => this.onPressSocialButton('wa')}>
                    <Image source={require('./img/icono-whatsapp.png')} style={{width:70, height:70,margin:10}} />
                </TouchableHighlight>
                <TouchableHighlight onPress={() => this.onPressSocialButton('fb')}>
                    <Image source={require('./img/icono-like.png')} style={{width:70, height:70,margin:10}} />
                </TouchableHighlight>
                <TouchableHighlight onPress={() => this.onPressSocialButton('ingrm')}>
                    <Image source={require('./img/icono-like-instagram.png')} style={{width:70, height:70,margin:10}} />
                </TouchableHighlight>
            </View>
        )
    }
}

I've tried adding const { navigation:navigate } = this.props;within the onPress method, also i tried binding this in the constructor, like: this.onPressSocialButton = this.onPressSocialButton.bind(this); Nothing worked, please help me. thanks!

2条回答
霸刀☆藐视天下
2楼-- · 2019-08-27 01:55

Solved by adding: const { navigation } = this.props; in the parent component's render() method (Login.js) then i passed it to the child component like this:

<SocialFooter navigation={navigation}/>
查看更多
Lonely孤独者°
3楼-- · 2019-08-27 02:04

Navigation prop is only supplied to the react-navigation screens which you have already configured, if you need to use the navigation prop in any other components, you need to pass it as a prop

<SocialFooter navigation={navigation}/>

查看更多
登录 后发表回答