【问题标题】:undefined is not an object (evaluating '_this3.props.navigation.navigate')undefined 不是对象(评估 '_this3.props.navigation.navigate')
【发布时间】:2020-05-30 19:49:45
【问题描述】:

屏幕之间的堆栈导航问题。 我在“SveKategorije”屏幕上显示数据。 它基本上是类别按钮,当我单击按钮时,我现在只想显示另一个屏幕,但由于某种原因它不起作用。 当我把 onPress={() => this.props.navigation.navigate('screenname')} 它给了我这个 error 我正在使用 (react-native - 0.57.7)

这是 router.js 代码(我在其中声明我的路线)

import React from 'react';
import { View, Text, Button } from "react-native";
import { createBottomTabNavigator, createStackNavigator } from "react-navigation";
import { Icon } from 'react-native-elements';

//TABS
import Categories from '../tabs/categories';
import Home from '../tabs/home';
import Me from '../tabs/me';

//screens for CATEGORIES 
import ViceviPoKategoriji from '../components/Ispis/ViceviPoKategoriji';


//CATEGORIES STACK 
export const categoriesFeedStack = createStackNavigator({

    SveKategorije: {
        screen: Categories,
        navigationOptions: {
            title: 'KATEGORIJE',
        },
    },

    ViceviPoKategoriji: {
        screen: ViceviPoKategoriji,
    }

});


//TABS
export const Tabs = createBottomTabNavigator({


    Categories: {
        screen: categoriesFeedStack,
        navigationOptions: {
            title: 'Kategorije',
            label: 'Kategorije',
            tabBarIcon: ({ tintColor }) => <Icon name="list" size={35} color={tintColor} />,
        }

    },
    Home: {
        screen: Home,
        navigationOptions: {
            title: 'Pocetna',
            label: 'Kategorije',
            tabBarIcon: ({ tintColor }) => <Icon name="home" size={35} color={tintColor} />,
        }

    },
    Me: {
        screen: Me,
        navigationOptions: {
            title: 'Profil',
            label: 'Kategorije',
            tabBarIcon: ({ tintColor }) => <Icon name="account-circle" size={35} color={tintColor} />,
        }
    },

},
    {
        initialRouteName: "Home",
        showIcon: true
    },

);

这里是“SveKategorije”屏幕

import React from 'react';
import { StyleSheet, Text, View, ActivityIndicator, ScrollView, Button } from 'react-native';
import { createStackNavigator, createAppContainer, StackNavigator, navigate } from 'react-navigation';


export default class SveKategorije extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            isLoading: true,
            dataSource: null
        }
    }




    componentDidMount() {
        return fetch('http://centarsmijeha.com/api/allCategories')
            .then((response) => response.json())
            .then((responseJson) => {
                this.setState({
                    isLoading: false,
                    dataSource: responseJson.data,
                })
            })
            .catch((error) => {
                console.log(error)
            });
    }
    render() {

        if (this.state.isLoading) {
            return (
                <View style={styles.container}>
                    <ActivityIndicator />
                </View>
            )
        } else {
            let data = this.state.dataSource.map((val, key) => {

                return (
                    <View key={key} style={styles.item}>

                        <Button
                            onPress={() => this.props.navigation.navigate('ViceviPoKategoriji')}
                            title={val.categoryName}
                        />
                    </View>
                );

            });
            return (
                <ScrollView>

                    {data}
                </ScrollView >
            );
        }
    }
}




//CSS
const styles = StyleSheet.create({
    container: {
        flex: 1,
        backgroundColor: '#fff',
        alignItems: 'center',
        justifyContent: 'center',
        width: '100%'
    },
    item: {
        flex: 1,
        alignSelf: 'stretch',
        width: '100%',
        textAlign: 'center',
        alignItems: 'center',
        justifyContent: 'center'
    }
});

这里是“ViceviPoKategoriji”屏幕(点击“SveKategorije”屏幕上的任何按钮时应显示的屏幕)

import React from 'react';
import { StyleSheet, Text, View, ActivityIndicator, ScrollView } from 'react-native';


export default class ViceviPoKategoriji extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            isLoading: true,
            dataSource: null,
        }
    }
    componentDidMount() {
        return fetch('http://centarsmijeha.com/api/jokesByCategory/16')
            .then((response) => response.json())
            .then((responseJson) => {
                this.setState({
                    isLoading: false,
                    dataSource: responseJson.data,
                })
            })
            .catch((error) => {
                console.log(error)
            });
    }
    render() {
        if (this.state.isLoading) {
            return (
                <View style={styles.container}>
                    <ActivityIndicator />
                </View>
            )
        } else {
            let data = this.state.dataSource.map((val, key) => {

                return <View key={key} style={styles.item}><Text>{val.jokeText}</Text></View>

            });
            return (
                <ScrollView>

                    {data}
                </ScrollView >
            );
        }
    }
}
const styles = StyleSheet.create({
    container: {
        flex: 1,
        backgroundColor: '#fff',
        alignItems: 'center',
        justifyContent: 'center',
    },
    item: {
        flex: 1,
        alignSelf: 'stretch',
        marginTop: 50,
        marginRight: '15%',
        marginLeft: '15%',
        width: '70%',
        textAlign: 'center',
        alignItems: 'center',
        justifyContent: 'center',
        borderBottomWidth: 1,
        borderBottomColor: '#eee'
    }
});

【问题讨论】:

  • SveKategorije 在您的路由器堆栈中不存在,因此就导航库而言,此屏幕不存在,这就是为什么当您尝试使用 this.props.navigation 时会发现它未定义

标签: javascript reactjs react-native


【解决方案1】:

React-navigation 是基于 props 的导航。 我认为您的组件没有导航道具。 请检查您的组件是否有导航道具。

render() {
  console.log(this.props.navigation)
  // or debugger
  return (

如果 console.log 的结果未定义,则将“import SveKategorije”添加到您的路由文件中。

【讨论】:

  • 导入“SveKategorije”并将其作为屏幕放入,为我工作
【解决方案2】:

您必须做更多的设置才能从组件导航。您可以通过 ref 访问导航器并将其传递给 NavigationService,我们稍后将使用它来导航。

https://reactnavigation.org/docs/en/navigating-without-navigation-prop.html

【讨论】:

    【解决方案3】:

    就我而言,我不小心在功能组件中使用了 this.props.navigation。如果有人犯了这样的错误,将再次检查您的代码。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-16
      • 2020-07-16
      • 2018-08-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多