错误:无法读取的未定义的属性“国家”

问题描述 投票:0回答:1

enter image description here该错误调试器和屏幕

我创建从本地API博客文章与点击“更多”按钮定位到检索根据id其余项目的目的。但是,我利用导航,但它给了未定义“无法读取属性‘国家’的错误。

import React, {Component} from 'react';
import { StyleSheet, Text, View} from 'react-native';
import Post from './components/Post';
import PostSingle from './components/PostSingle';
import { createStackNavigator, createAppContainer } from 'react-navigation';

const RootStack = createStackNavigator(
  {
    PostScreen: { screen: Post},
    PostSingleScreen:{screen: PostSingle},

  }
);

const AppNavigator = createAppContainer(RootStack);
export default class App extends Component {
  constructor(props) {
    super(props);
  };
  render() {
    return (
      <View style={styles.container}>
        <AppNavigator/>
      </View>
    );
  }
}

Post.js屏幕载荷POST从其中读更多按钮被按下以触发PostSingle

import React, { Component } from 'react';
import {
    ScrollView, 
    StyleSheet,
    View, 
    Text,
    InputText,
    TouchableOpacity
} from 'react-native';
import axios from 'axios';

export default class Post extends Component{
    constructor(props){
        super(props);
        this.state = {
            posts: []
        }
    }
     readMore = (id) => {
         this.setState({ id: id})
         this.props.navigation.navigate('PostSingleScreen')
     } 

    componentDidMount(){
        axios.get(`http://localhost/rest_api_myblog/api/post/read.php`)
        //.then(json => console.log(json.data.data[0].id))
        .then(json => json.data.data.map(mydata =>(
            {
                title: mydata.title,
                body: mydata.body,
                author: mydata.author,
                category_name: mydata.category_name, 
                id: mydata.id 
            }
        )))
        //.then(newData => console.log(newData))
       .then(newData => this.setState({posts: newData}))
       .catch(error => alert(error))

        }

    render(){
        return (
        <View>

        <ScrollView style={styles.scrollContent}>
            <View style={styles.header}>
                <Text style={styles.headerText}>Gist Monger</Text>
            </View> 
             {   
                 this.state.posts.map((post, index) =>(
                    <View key={index} style={styles.container}>
                        <Text style={styles.display}>
                            Author:  {post.author}
                        </Text>
                        <Text style={styles.display}>
                            Category: {post.category_name}
                        </Text>
                        <Text style={styles.display}>
                            Title: {post.title}
                        </Text>
                        <Text style={{overflow:'hidden'}}>
                            Id: {post.id}
                        </Text>
                     <TouchableOpacity style={styles.buttonContainer}
                     onPress = {() => this.readMore(post.id)}
                     >
                        <Text style={styles.buttonText}>

                            Read More
                        </Text>
                     </TouchableOpacity>

                     </View> 
                 ))
             }

        </ScrollView>
            <View style={styles.footer}></View>
        </View>
        );
    }
}

PostSingle.js的PostSingle预计基于点击后该项目的ID来显示一个职位。

import React, { Component } from 'react';
import {
    ScrollView, 
    StyleSheet,
    View, 
    Text,
    TouchableOpacity
} from 'react-native';
import axios from 'axios';

export default class Post extends Component{
    constructor(props){
        super(props);
        this.state = {
            posts: []
        }
    }


    componentDidMount(){
        axios.get(`http://localhost/rest_api_myblog/api/post/read_single.php?id=${id}`)
      .then(json => json.data.data.map(mydata =>(
            {
                title: mydata.title,
                body: mydata.body,
                author: mydata.author,
                category_name: mydata.category_name, 
                body: mydata.body 
            }
        )))
       .then(newData => this.setState({posts: newData}))
       .catch(error => alert(error))

        }

    render(){
        return (
        <View>

        <ScrollView style={styles.scrollContent}>
            <View style={styles.header}>
                <Text style={styles.headerText}>Gist Monger</Text>
            </View> 
             {   
                 this.state.posts.map((post, index) =>(
                    <View key={index} style={styles.container}>
                        <Text style={styles.display}>
                            Author:  {post.author}
                        </Text>
                        <Text style={styles.display}>
                            Category: {post.category_name}
                        </Text>
                        <Text style={styles.display}>
                            Title: {post.title}
                        </Text>
                        <Text style={{overflow:'hidden'}}>
                            Body: {post.body}
                        </Text>
                     </View> 
                 ))
             }

        </ScrollView>
            <View style={styles.footer}></View>
        </View>
        );
    }
}
react-native axios react-navigation-stack
1个回答
0
投票

你必须与react-native-gesture-handler一起安装react-navigation。见this

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.