【问题标题】:React Native - navigation issue “undefined is not an object (t.props.navigation.navigate)”React Native - 导航问题“未定义不是对象(t.props.navigation.navigate)”
【发布时间】:2019-06-12 15:02:17
【问题描述】:

我目前正在创建一个小型 Magic the Gathering 项目,该项目从 API 获取卡片数据并将其插入到我的应用程序中。我首先获取了名称等数据,但是当我单击名称时,我想查看更多信息,例如卡片描述等。但是,我一直遇到错误“未定义不是对象(this.props.navigation.navigate)”的问题。我该如何解决这个问题?

import React from 'react';
import { Paragraph, Card, Avatar, Button } from "react-native-paper";
import { Image, View, StyleSheet, AsyncStorage } from 'react-native';


export default class CardProfile extends React.Component {
  render() {
    const { props } = this.props
    const card = props["state"].params
    const img = card.imageUrl
    return (
      <View style={styles.container}>

          <View style={styles.cardinfo}>

                    <Card>
                        <View style={styles.title}>
                            <Card.Title
                                title={card.name}
                                subtitle={card.colors}
                            />
                        </View>

                        <View style={styles.content}>
                            <Card.Content>
                                <Paragraph>{card.text}</Paragraph>
                                <Image
                                    style={styles.img}
                                    source={{ uri: img }}
                                />
                            </Card.Content>
                        </View>

                        <View style={styles.actions}>
                            <Card.Actions>
                                <Button onPress={() => {
                                    // create a function that saves your data asyncronously
                                    _storeData = async () => {
                                        try {
                                            await AsyncStorage.setItem('@Card', this.state.card);
                                        } catch (error) {
                                            console.log("Could not save!", error);
                                        }
                                    }
                                }}>add card</Button>
                            </Card.Actions>
                        </View>

                    </Card>

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


输出应该是我想要查看的所选卡片的卡片信息。

【问题讨论】:

  • 您不会在您共享的代码中的任何地方调用导航。你能在你调用它的地方包含代码吗?

标签: reactjs api react-native react-navigation react-native-paper


【解决方案1】:

您在代码中的什么地方使用了this.props.navigation.navigate?你确定你调用它的组件有navigation作为prop传递吗?

【讨论】:

  • 我在获取我认为的 api 的地方使用它,` data={this.state.dataSource} renderItem={({item}) => this.props.navigation.navigate('CardProfile', item)}>`
  • 好的。如果您在该行(或 console.log )上放置一个断点,this.props.navigation 值是否未定义?组件可能没有收到 prop,您需要从导航器堆栈中的父组件显式传递它(例如
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多