【问题标题】:React native List.Item undefined is not an objectReact native List.Item undefined is not an object
【发布时间】:2020-08-07 10:59:27
【问题描述】:

大家好,我的 List.item 有问题,代码如下:

我不知道问题出在哪里

import React from 'react';
import {
    Avatar,
    Button,
    Card,
    Title,
    Paragraph,
    List,
} from 'react-native-paper';
import HTML from 'react-native-htmlview';
import {
    View,
    ScrollView,
    ActivityIndicator
} from 'react-native';
import moment from 'moment';

export default class SinglePost extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            isloading: true,
            post: [],
        };
    }
    componentDidMount() {
        this.fetchPost();
    }
    async fetchPost() {
        let post_id = this.props.route.params?.post_id
        const response = await fetch(
            `https://kriss.io/wp-json/wp/v2/posts?_embed&include=${post_id}`
        );
        const post = await response.json();
        this.setState({
            post: post,
            isloading: false,
        });
    }
render() {
    let post = this.state.post;

    return (
        <ScrollView>
            <Card>
                <Card.Content>
                    <Title>{post[0].title.rendered}</Title>
                    <List.Item
                        title={`${post[0]._embedded.author[0].name}`}
                        description={`${post[0]._embedded.author[0].description}`}
                        left={props => {
                            return (
                                <Avatar.Image
                                    size={55}
                                    source={{
                                        uri: `${post[0]._embedded.author[0].avatar_urls[96]}`,
                                    }}
                                />
                            );
                        }}
                    />
                    <List.Item
                        title={`Published on ${moment(
                            post[0].date,
                            'YYYYMMDD'
                        ).fromNow()}`}
                    />
                    <Paragraph />
                </Card.Content>
                <Card.Cover source={{ uri: post[0].jetpack_featured_media_url }} />
                <Card.Content>
                    <HTML value={post[0].content.rendered} addLineBreaks={false} />
                </Card.Content>
            </Card>
        </ScrollView>
    );
}
}

我的目标是在卡片组件中显示来自 wordpress 博客的帖子到我的单个帖子页面,但我不断收到此错误:TypeError: undefined is not an object (evaluation 'post[0].title' )

感谢您的帮助:)

【问题讨论】:

    标签: json api react-native object undefined


    【解决方案1】:

    您无法使用 let post = this.state.post; 获取状态

    你可以像这样使用“destructuring assignment

    export default class SinglePost extends React.Component {
      ...
      render() {
        const { post, isloading} = this.state;   //Destructuring assignment from state
        //...
    
         <Title>{post[0].title.rendered}</Title>  //Then get it
    
        //...
      }
    }
    

    【讨论】:

    • 谢谢你的回答我试过了没用我不明白
    • 类似this
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-30
    • 2020-08-31
    相关资源
    最近更新 更多