React- redux tutorial - TypeError: Cannot read properties of undefined (reading 'map')

React- redux tutorial - TypeError: Cannot read properties of undefined (reading 'map')

我是 React 的新手,所以我尝试按照 React redux 教程进行操作并遇到此错误 - 类型错误:无法读取未定义的属性(阅读 'map')

如果有人能帮上忙,我将不胜感激!

下面是我的代码

    import React, {useState} from 'react'
import { useSelector } from 'react-redux'
import { Link } from 'react-router-dom'


export const PostsList = () => {
const posts = useSelector(state => state.posts)


const renderedPosts = posts.map(posts => (
    <article className="post-excerpt" key={posts.id}>
    <h3>{posts.title}</h3>
    <p className="post-content">{posts.content.substring(0, 100)}</p>
    <Link to={`/posts/${posts.id}`} className="button muted-button">
    View Post
    </Link>
    </article>
))

return (
    <section className="posts-list">
    <h2>Posts</h2>
    {renderedPosts}
    </section>
)
}

我认为您没有从 useSelector 中获得任何价值。我的意思是 posts 变量初始化为未定义。这就是为什么您收到错误无法读取未定义的属性(读取 'map')的原因。 因为 map 不能在 undefined 上工作。