undefined is not an object (evaluating '_this.props.navigation.navigate')

风格不统一 提交于 2019-12-11 17:46:56

问题


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!


回答1:


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}/>



回答2:


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}/>



来源:https://stackoverflow.com/questions/51064347/undefined-is-not-an-object-evaluating-this-props-navigation-navigate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!