Advertisement
Guest User

Untitled

a guest
Apr 3rd, 2020
186
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.   // This part feels Janky,
  6.   //should I just pass data and setData from the parent function's useState instead?
  7.   const { data } = props;
  8.   const [rowData, setRowData] = useState([]);
  9.  
  10.   useEffect(() => {
  11.     setRowData(data); //setData()
  12.   }, [data]);
  13.  
  14.   function clickSort(key) {
  15.     setRowData([...rowData].sort(sortBy(key))); //setData()
  16.   }
  17.  
  18.   const table = rowData.map((row, index) => {
  19.     return <div key={index}>{row.name}</div>;
  20.   });
  21.  
  22.   return (
  23.     <div>
  24.       <button onClick={() => clickSort("name")}>Click to sort by Name</button>
  25.       <button onClick={() => clickSort("id")}>Click to sort by ID</button>
  26.       {table}
  27.     </div>
  28.   );
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement