Creating a full Reddit clone is a large project, but I can help you get started with a basic React app styled with Tailwind CSS that mimics the Reddit homepage layout and some core features like posts listing, upvote/downvote buttons, and a sidebar.
Here’s a simple example to get you started:
Step 1: Setup React with Tailwind CSS
If you don’t have a React app set up yet, create one with Vite (fast and modern):
npm create vite@latest reddit-clone -- --template react
cd reddit-clone
npm install
Then install Tailwind CSS:
npm install -D tailwindcss postcss autoprefixer
npx tailwindcss init -p
Configure tailwind.config.js:
/** @type {import('tailwindcss').Config} */
module.exports = {
content: [
"./index.html",
"./src/**/*.{js,jsx,ts,tsx}",
],
theme: {
extend: {},
},
plugins: [],
}
Add Tailwind directives to src/index.css:
@tailwind base;
@tailwind components;
@tailwind utilities;
Step 2: Basic Reddit-like Layout in React
Replace src/App.jsx with this:
import { useState } from "react";
const postsData = [
{
id: 1,
title: "Welcome to Reddit Clone!",
subreddit: "r/reactjs",
author: "user123",
votes: 10,
comments: 5,
time: "2 hours ago",
},
{
id: 2,
title: "Tailwind CSS is awesome",
subreddit: "r/tailwindcss",
author: "cssfan",
votes: 25,
comments: 12,
time: "1 hour ago",
},
];
function Post({ post, onVote }) {
return (
<div className="flex border border-gray-300 rounded-md p-4 mb-4 bg-white">
<div className="flex flex-col items-center mr-4 text-gray-500">
<button
onClick={() => onVote(post.id, 1)}
className="hover:text-red-500"
aria-label="Upvote"
>
▲
</button>
<span className="font-bold">{post.votes}</span>
<button
onClick={() => onVote(post.id, -1)}
className="hover:text-blue-500"
aria-label="Downvote"
>
▼
</button>
</div>
<div>
<div className="text-sm text-gray-500 mb-1">
<span className="font-semibold">{post.subreddit}</span> • Posted by u/{post.author} • {post.time}
</div>
<h2 className="text-lg font-semibold mb-2">{post.title}</h2>
<div className="text-sm text-gray-600">{post.comments} comments</div>
</div>
</div>
);
}
function Sidebar() {
return (
<div className="w-64 p-4 bg-gray-50 rounded-md sticky top-4 h-fit">
<h3 className="font-bold text-lg mb-4">Popular Communities</h3>
<ul className="space-y-2">
<li className="hover:bg-gray-200 rounded px-2 cursor-pointer">r/reactjs</li>
<li className="hover:bg-gray-200 rounded px-2 cursor-pointer">r/tailwindcss</li>
<li className="hover:bg-gray-200 rounded px-2 cursor-pointer">r/javascript</li>
<li className="hover:bg-gray-200 rounded px-2 cursor-pointer">r/webdev</li>
</ul>
</div>
);
}
export default function App() {
const [posts, setPosts] = useState(postsData);
const handleVote = (id, delta) => {
setPosts((posts) =>
posts.map((post) =>
post.id === id ? { ...post, votes: post.votes + delta } : post
)
);
};
return (
<div className="min-h-screen bg-gray-100">
<header className="bg-white shadow p-4 flex items-center justify-between">
<h1 className="text-2xl font-bold text-red-600">reddit</h1>
<nav>
<ul className="flex space-x-4 text-gray-700">
<li className="hover:underline cursor-pointer">Home</li>
<li className="hover:underline cursor-pointer">Popular</li>
<li className="hover:underline cursor-pointer">All</li>
</ul>
</nav>
</header>
<main className="max-w-6xl mx-auto p-4 flex space-x-6">
<section className="flex-1">
{posts.map((post) => (
<Post key={post.id} post={post} onVote={handleVote} />
))}
</section>
<aside>
<Sidebar />
</aside>
</main>
</div>
);
}
What this does:
- A header with a Reddit-style logo and nav links.
- A main content area with a list of posts.
- Each post has upvote/downvote buttons that update the vote count.
- A sidebar with popular communities.
- Tailwind CSS for styling and layout.
Step 3: Run your app
npm run dev
Open the URL shown in your terminal (usually http://localhost:5173) and you should see a simple Reddit-like homepage.
Next steps to improve:
- Add routing with React Router for subreddits and post pages.
- Add user authentication.
- Add post creation form.
- Fetch posts from a backend or API.
- Add comments and nested replies.
- Improve UI with icons (e.g., react-icons) and animations.
If you want, I can help you build out any of these features next! Would you like to add routing or a post submission form?