1

Im new in react so i try to follow react redux tutorial and got this error - TypeError: Cannot read properties of undefined (reading 'map')

I'm really appreciate if anyone could help thank you!

below are my code

    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>
)
}

1 Answer 1

2

I think you are not getting any value from useSelector. I mean posts variable is initialed to undefined. That is why you are getting an error Cannot read properties of undefined (reading 'map'). Because map can not work on undefined.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.