I am having an error when refreshing my browser.
Uncaught TypeError: Cannot read properties of undefined (reading 'title')
And it keeps on repeating for all of my states (title
, content
, id
) until I delete them from code down below... And also it works fine until I refresh the page.
import React, { FC, useEffect } from 'react';
import { useLocation, useParams } from 'react-router-dom';
import AddComment from 'src/components/ui/AddComment/AddComment';
import CommentSection from 'src/components/ui/CommentSection/CommentSection';
import PostContent from 'src/components/ui/PostContent/PostContent';
import PostHeader from 'src/components/ui/PostHeader/PostHeader';
import {useAppSelector } from 'src/store/app/hooks';
const Post: FC = () => {
const { id } = useParams();
const { posts } = useAppSelector((state) => state.post);
return (
<div>
<PostHeader
header={<h2>{posts.find(p => p.id === parseInt(id)).title}</h2>}
>
<div>
{posts.find(p => p.id === parseInt(id)).content}
</div>
</PostHeader>
<PostContent
content={<div>{posts.find(p => p.id === parseInt(id)).content}</div>}
/>
<AddComment id={id} />
<CommentSection id={id} />
</div>
)
};
export default Post;
I also want to stop posts from dissapearing after refresh.