如何解决类型上不存在属性 - 无法在组件中获取道具
我是 React 新手,我不知道如何将 props 发送到组件
通过 axios 从 wordpress 站点获取帖子后,获取调用并映射响应
我无法通过“点”post.id
访问该元素,而只能作为数组 post['id']
{posts.map(post => (
// console.log(post.id)
// the log return Property 'id' does not exist on type 'never'
<PostItem key={post['id']} post={post}/>
))}
在 PostItem 组件中,我得到了道具 post 和 key 但我不能像 post.id 或 post.title 那样获取帖子的数据
知道如何正确使用道具吗?
谢谢
这是我的 PostItem 组件的完整代码
import React,{ Component } from 'react'
// import axios from 'axios'
type Props = {
post: any[];
key: string;
}
export class PostItem extends React.Component<Props>{
render() {
// const {post} = this.props.post; // return : Property 'post' does not exist on type 'any[]'
const post = this.props.post;
console.log(post); //return the post data
console.log(post.id); // return Property 'id' does not exist on type 'any[]'
//console.log(post['id']); // return Element implicitly has an 'any' type because index expression is not of type 'number'. TS7015
return (
<div></div>
)
}
}
export default PostItem
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。