I am trying to render posts by mapping over the posts array and then past individual post to Post component. However it prints the posts data on the console but doesnt render it on the screen.
import React from "react"
import Post from './Post/Post';
import { ThemeProvider } from "@mui/material/styles"
import theme from "./styles"
import { useSelector } from "react-redux"
import {Grid, CircularProgress} from '@mui/material'
const Posts = ({setCurrentId})=> {
const posts = useSelector((state) => state.posts)
console.log(posts);
return (
!posts.length ? <CircularProgress /> : (
<Grid className={theme.mainContainer} container alignItems="stretch" spacing={3}>
{posts.map((post) => (
<Grid key={post._id} item xs={12} sm={6} md={6}>
<Post post={post} setCurrentId={setCurrentId} />
</Grid>
))}
</Grid>
)
);
};
export default Posts;
I have tried optional chaining to check if the array exists. Bu i still get the same eror.
The error on screen says "Cannot read properties of undefined (reading ‘map’)
TypeError: Cannot read properties of undefined (reading ‘map’) ". On the console I get this error “Posts.js:15 Warning: Each child in a list should have a unique “key” prop.”