Advertisement
Guest User

Untitled

a guest
Apr 3rd, 2020
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { useState, useEffect } from "react";
  2. import { sortBy } from "../../utils/sortBy";
  3.  
  4. export default function Table(props) {
  5.   const { data, setData } = props;
  6.   const [rowData, setRowData] = useState([]);
  7.  
  8.   function clickSort(key) {
  9.     setData([...rowData].sort(sortBy(key)));
  10.   }
  11.  
  12.   const table = rowData.map((row, index) => {
  13.     return <div key={index}>{row.name}</div>;
  14.   });
  15.  
  16.   return (
  17.     <div>
  18.       <button onClick={() => clickSort("name")}>Click to sort by Name</button>
  19.       <button onClick={() => clickSort("id")}>Click to sort by ID</button>
  20.       {table}
  21.     </div>
  22.   );
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement