I’m passing some data to <Outlet />
and it works fine. Then receiving via useOutletContext
hook. Sometimes it gave me expected results, but sometime it gave me error.
routes
<Route path="profile/:username" element={<About />}>
<Route index element={<UserPosts />} />
<Route path="saved" element={<BookmarksList />} />
<Route path="liked-posts" element={<UserLikedPosts />} />
</Route>
I’m passing calculated data to Outlet
, this <Outlet />
is going to render list of posts according to active tab. For “posts” and “likes” it works as expected, but if I go for “Saved” tab, it gave me error sometime. Sometimes it works properly.
const profileData = {
savedPosts: profile?.savedPosts,
ownPosts: posts,
likedPosts: currentUser?.likedPosts
}
<div>
<Outlet context={[profileData]} />
</div>
const personalAccTabs = [
{
path: "",
text: "Posts"
},
{
path: "liked-posts",
text: "Likes"
},
{
path: "saved",
text: "Saved"
}
];
Posts list component
import PostOverlay from "../../ui/PostOverlay";
import { useOutletContext } from "react-router-dom";
const BookMarkList = () => {
// const context = useOutletContext()
// console.log(context)
const [{savedPosts}] = useOutletContext()
return (
<ul className="grid gap-2 py-2 lg:grid-cols-3 xl:grid-cols-4 md:grid-cols-2 max-sm:gap-y-2">
{
savedPosts && savedPosts?.map(post => <figure key={post._id} className=" rounded *:rounded max-h-[345px] h-[345px] relative group">
<img src={post.mediaUrl} alt="saved post image" className="object-cover w-full h-full" />
<PostOverlay comments={post?.comments?.length} likes={post?.likes?.length}/>
</figure>)
}
</ul>
);
};
export default BookMarkList;
The error I’m facing is:
Uncaught (in promise) TypeError: useOutletContext is not a function or
its return value is not iterable at BookMarkList
(BookmarksList.jsx:7:26).
I’ve visited the docs as well and tried to follow as they instructs. now I’m not getting the point why it throws error like this.
1