We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Prepare
- React
- Data Handling
- Article Sorting
- Discussions
Article Sorting
Article Sorting
Sort by
recency
|
21 Discussions
|
Please Login in order to post a comment
Easy Answer
App
Artciles
all testcases are passed
App.jsx
Article,jsx
//App.jsx
import "h8k-components";
import Articles from "./components/Articles"; import {useState} from "react"; import {useEffect} from "react";
import "./App.css";
function App({ articles }) { // first method // const initialState = ()=>{ // const sorted = [...articles].sort(function(b, a){ // if(a.upvotes > b.upvotes) return 1; // else if (a.upvotes < b.upvotes) return -1; // else return 0; // });
// return sorted; // }
// const [sortedArticle, setSortedArticle] = useState(initialState());
//second method useEffect
const [sortedArticle, setSortedArticle] = useState(articles);
useEffect(()=> {
} , []);
const handleMostUpvoted = () => { // Logic for most upvoted articles
};
const handleMostRecent = () => { // Logic for most recent articles const sortDate = [...sortedArticle].sort((a, b)=>{ if(a.date < b.date) return 1; else if (a.date > b.date) return -1; else return 0; });
}; return ( <> Sort By Most Upvoted Most Recent ); }
export default App;
//Articles.jsx
import React from "react";
function Articles({ articles = [] }) { return ( {articles.map((article, index) => ( ))}
Make sure not to change "data-testid" leave as it is ); } ma export default Articles;
All tests passed:
import "h8k-components";
import Articles from "./components/Articles";
import "./App.css"; import { useState } from "react";
function App({ articles }) { const [sortedArticles, setSortedArticles] = useState( [...articles].sort((a, b) => b.upvotes - a.upvotes) );
const handleMostUpvoted = () => { // Logic for most upvoted articles const updated = [...sortedArticles].sort((a, b) => b.upvotes - a.upvotes); setSortedArticles(updated); };
const handleMostRecent = () => { // Logic for most recent articles const updated = [...sortedArticles].sort( (a, b) => new Date(b.date) - new Date(a.date) ); setSortedArticles(updated); }; return ( <> Sort By Most Upvoted Most Recent ); }
export default App;
import React from "react";
function Articles({ articles }) { return ( Title Upvotes Date
); }
export default Articles;