react make an ascending descending button

Solutions on MaxInterview for react make an ascending descending button by the best coders in the world

showing results for - "react make an ascending descending button"
Antonio
03 Sep 2018
1import React from 'react';
2import './styles.css';
3
4const useSortableData = (items, config = null) => {
5  const [sortConfig, setSortConfig] = React.useState(config);
6
7  const sortedItems = React.useMemo(() => {
8    let sortableItems = [...items];
9    if (sortConfig !== null) {
10      sortableItems.sort((a, b) => {
11        if (a[sortConfig.key] < b[sortConfig.key]) {
12          return sortConfig.direction === 'ascending' ? -1 : 1;
13        }
14        if (a[sortConfig.key] > b[sortConfig.key]) {
15          return sortConfig.direction === 'ascending' ? 1 : -1;
16        }
17        return 0;
18      });
19    }
20    return sortableItems;
21  }, [items, sortConfig]);
22
23  const requestSort = (key) => {
24    let direction = 'ascending';
25    if (
26      sortConfig &&
27      sortConfig.key === key &&
28      sortConfig.direction === 'ascending'
29    ) {
30      direction = 'descending';
31    }
32    setSortConfig({ key, direction });
33  };
34
35  return { items: sortedItems, requestSort, sortConfig };
36};
37
38const ProductTable = (props) => {
39  const { items, requestSort, sortConfig } = useSortableData(props.products);
40  const getClassNamesFor = (name) => {
41    if (!sortConfig) {
42      return;
43    }
44    return sortConfig.key === name ? sortConfig.direction : undefined;
45  };
46  return (
47    <table>
48      <caption>Products</caption>
49      <thead>
50        <tr>
51          <th>
52            <button
53              type="button"
54              onClick={() => requestSort('name')}
55              className={getClassNamesFor('name')}
56            >
57              Name
58            </button>
59          </th>
60//...
61        </tr>
62      </thead>
63    </table>
64  );
65};
66
queries leading to this page
sort buttons reacthow to sort in react jsreact native sort data to decendingreact sort descending syntaxreactjs ascending order in arrayhow to sort data in react jsreact sort button componentssort this state reactsort array in ascnding order on price in reactreact sort ascendingorderby ascending reactsort data in react jsonclick should do ascending and descending sorting react jssort react jssort in react jssort in ascending and descending toggle in reactjsorder by in react jssort product with price in reactjshow to sort data in ascending descending order in react jsshort desc order in dropdown list in reactsort numbers decending reactorderby react react jsascending 2fdescending order button reactorderby reactdescending items in reactreact sort button componentorder by react jsfilter in react in disecending orderhow to do sorting in react jsadd sort in react apponclick sortbyorder by clause reactsorting in react jssorting elements by ascending and descending in reactjs using hookshow to set array in descending order reactjssort in reactreact state order desendprops to descending order in function component reactsort data in reactsorting reactsort data in ascending and descending toggle in reactjshow to sort by price in react jsreact make an ascending descending buttonfilter in react in ascending ordersort using reactsort feature in reactjssorting data usign react jsascending sort in react jshow to do sorting based on price in cards in reactreact sprt data by ascendingreact make an ascending descending button