【发布时间】: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