import { useEffect, useState } from 'react'; const API_BASE = 'http://localhost:8000'; const Home = () => { const [posts, setPosts] = useState([]); const [apiError, setApiError] = useState(false); const [loading, setLoading] = useState(true); useEffect(() => { const fetchPosts = async () => { setLoading(true); try { const res = await fetch(`${API_BASE}/posts`); if (!res.ok) throw new Error('API unavailable'); const data = await res.json(); setPosts(data); setApiError(false); } catch { setApiError(true); } finally { setLoading(false); } }; fetchPosts(); }, []); return (
{/* Header */}
Community

Community

Simple Social Media Application

{/* API error indication */} {apiError && (
Backend API is unavailable or unreachable.
)} {/* Posts List */}
{loading && !apiError && (
Loading posts...
)} {!loading && !apiError && (
{posts.length === 0 ? (
No posts found.
) : ( posts.map(post => (
{post.username} {post.username} {new Date(post.createdAt).toLocaleString()}
{post.content}
{post.likes} {post.comments}
)) )}
)}
); }; export default Home;