Currently, I'm able to render the posts on screen but how can I render using the select dropdown where the user can select different sort options like title, author, and description, and the posts get sorted accordingly? What should I change in order for it works any suggestions will be much appreciated. I have added both HomePage.js and Post.js which I'm using.
//HomePage.js
import React, { useState, useEffect, useMemo } from "react";
import Post from "../../Components/Post/Post";
import "./HomePage.css";
import axios from "axios";
const HomePage = () => {
const [posts, setPosts] = useState("");
let config = { Authorization: "..................." };
const url = "........................";
useEffect(() => {
AllPosts();
}, []);
const AllPosts = () => {
axios
.get(`${url}`, { headers: config })
.then((response) => {
const allPosts = response.data.articles;
setPosts(allPosts);
console.log(response);
})
.catch((error) => console.error(`Error: ${error}`));
};
const newPostsByTitle = useMemo(() => {
return [...posts].sort((a, b) => a.title.localeCompare(b.title));
}, [posts]);
console.log("sorted by title", newPostsByTitle);
const newPostsByAuthor = useMemo(() => {
return [...posts].sort((a, b) => a.author.localeCompare(b.author));
}, [posts]);
console.log("sorted by author", newPostsByAuthor);
const newPostsByDescription = useMemo(() => {
return [...posts].sort((a, b) =>
a.description.localeCompare(b.description)
);
}, [posts]);
console.log("sorted by description", newPostsByDescription);
return (
<div className="home">
<div className="select">
<select
name="slct"
id="slct"
onChange={(e) => AllPosts(e.target.value)}
>
<option value="Title">Title</option>
<option value="Author">Author</option>
<option value="Description">Description</option>
</select>
</div>
<Post className="Posts" posts={posts} key={posts.title} />
</div>
);
};
export default HomePage;
//Post.js
import React from "react";
import "./Post.css";
import { Fragment } from "react";
const Post = (props) => {
const displayPosts = (props) => {
const { posts } = props;
if (posts.length > 0) {
return posts.map((post) => {
return (
<Fragment>
<div className="Post" key={post.title}>
<img
src={post.urlToImage}
alt="covid"
width="100%"
className="img"
/>
<h5 className="title"> {post.title}</h5>
<p className="author"> {post.author}</p>
<p className="description"> {post.description}</p>
</div>
</Fragment>
);
});
}
};
return <div className="Posts">{displayPosts(props)}</div>;
};
export default Post;