首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >TypeError:未定义的对象不是对象(计算‘_reactNative.route.params)-反应导航5.0

TypeError:未定义的对象不是对象(计算‘_reactNative.route.params)-反应导航5.0
EN

Stack Overflow用户
提问于 2020-08-02 21:58:24
回答 1查看 1.2K关注 0票数 0

我知道S.O.还有一些与我的问题类似的问题,但它们对我不起作用。

我正在尝试将变量从主屏幕发送到另一个后续屏幕。但是,我得到了错误:

未定义的对象(evaluating'_reactNative.route.params)不是TypeError

我不确定这是否与我如何编写将参数传递给ReactiveNavigation5.0中的路由的代码有关。但是我看了文档,代码在我看来很好。

这是我的代码:

Home.JS

代码语言:javascript
复制
import { StatusBar } from 'expo-status-bar';
import React from 'react';
import { StyleSheet, Text, View, Button, navigation } from 'react-native';
import Follow from './Follow';

export default class Home extends React.Component {

    constructor(props) {
        
        super(props);
        this.state = {
            followRequest: ['John', 'Jane', 'Ram', 'Janice'],
            following: ['Hitesh']
        };
    }

    doFollow = index => {
        const { followRequest, following } = this.state;

        const followNew = followRequest.splice(index, 1);

        following.push(followNew)

        this.setState({
            followRequest,
            following
        });
    };

    render() {

        console.log(this.props.navigation)

    return (
        <View style={styles.container}>
            <Text>You are following {this.state.following.length}</Text>
            <Text>You have {this.state.followRequest.length} follow requests</Text>
            <Button
                title='Follow Page'
                onPress={() => {

                    this.props.navigation.navigate('Follow', {
                        
                        followRequest: this.state.followRequest,
                        following: this.state.following,
                        doFollow: this.doFollow()

                    });
                }}
            />
        </View>
            );
    }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#fff',
    alignItems: 'center',
    justifyContent: 'center',
  },
});

Follow.JS

代码语言:javascript
复制
import { StatusBar } from 'expo-status-bar';
import React from 'react';
import { StyleSheet, Text, View, Button, TouchableOpacityBase, route, navigation} from 'react-native';


export default class Follow extends React.Component {


    render() {

        const { followRequest }  = route.params;
        const { doFollow }  = route.params;

    return (
        <View style={styles.container}>

            <Text>followRequest: {JSON.stringify(followRequest)}</Text>   
          
        <Button
                title='Home Page'
                onPress={() => {
                    this.props.navigation.navigate('Home')
                } }
            />
        </View>
            );
    }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#fff',
    alignItems: 'center',
    justifyContent: 'center',
  },
});

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-08-03 01:05:05

您正在从“react原生”导入routenavigation。它们应该是使用this.props作为道具访问的。

代码语言:javascript
复制
import { StatusBar } from 'expo-status-bar';
import React from 'react';
import { StyleSheet, Text, View, Button, TouchableOpacityBase } from 'react-native';


export default class Follow extends React.Component {


    render() {

        const { followRequest }  = this.props.route.params;
        const { doFollow }  = this.props.route.params;

    return (
        <View style={styles.container}>

            <Text>followRequest: {JSON.stringify(followRequest)}</Text>   
          
        <Button
                title='Home Page'
                onPress={() => {
                    this.props.navigation.navigate('Home')
                } }
            />
        </View>
            );
    }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#fff',
    alignItems: 'center',
    justifyContent: 'center',
  },
});

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63221428

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档