Create a React.js Social Media Feed App
Q: Build a React.js-based social media feed application that fetches and displays user posts, including features such as post creation, commenting, and liking.
- ReactJS
- Senior level question
Explore all the latest ReactJS interview questions and answers
ExploreMost Recent & up-to date
100% Actual interview focused
Create ReactJS interview for FREE!
Sure, here's an example of a simple social media feed application built with React.js, which fetches and displays user posts, and includes features such as post creation, commenting, and liking.
First, let's create a Post component to display individual posts:
import React, { useState } from 'react'; const Post = ({ post, handleComment, handleLike }) => { const [comment, setComment] = useState(''); const handleCommentSubmit = (e) => { e.preventDefault(); handleComment(comment); setComment(''); } return ( <div className="post"> <div className="post-header"> <img src={post.userAvatar} alt="user avatar" /> <div className="post-header-info"> <h3>{post.username}</h3> <p>{post.date}</p> </div> </div> <div className="post-content"> <p>{post.content}</p> <img src={post.image} alt="post image" /> </div> <div className="post-actions"> <button onClick={() => handleLike(post.id)}>Like</button> <button>Share</button> </div> <div className="post-comments"> <ul> {post.comments.map((comment, index) => ( <li key={index}> <span>{comment.username}:</span> {comment.text} </li> ))} </ul> <form onSubmit={handleCommentSubmit}> <input type="text" value={comment} onChange={(e) => setComment(e.target.value)} placeholder="Add a comment" /> <button type="submit">Post</button> </form> </div> </div> ); } export default Post;
The Post component takes in a post object as a prop, which includes information such as the user's avatar, username, date, content, image, and comments. It also takes in two callback functions, handleComment and handleLike, which are triggered when the user comments on or likes the post.
The handleComment function takes in the comment text as a parameter and adds it to the post's comments array. The handleLike function takes in the post ID as a parameter and updates the post's liked state.
Next, let's create a Feed component to display a list of posts:
import React, { useState } from 'react'; import Post from './Post'; const Feed = () => { const [posts, setPosts] = useState([ { id: 1, userAvatar: 'https://via.placeholder.com/50', username: 'John Doe', date: '2022-01-01', content: 'This is my first post!', image: 'https://via.placeholder.com/500', comments: [] }, { id: 2, userAvatar: 'https://via.placeholder.com/50', username: 'Jane Smith', date: '2022-01-02', content: 'Check out this cool picture!', image: 'https://via.placeholder.com/500', comments: [] } ]); const handleComment = (postId, comment) => { setPosts(prevPosts => prevPosts.map(post => { if (post.id === postId) { return { ...post, comments: [ ...post.comments, { id: post.comments.length + 1, username: 'Guest', text: comment } ] } }


