Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

135
Views
Preserve order by after updating data in Material UI in React

When I select a column, the order is not preserved when the data is automatically reloaded and returns to the original order.

  const [data, setData] = useState([]);

  const [order, setOrder] = useState("asc");
  const [orderBy, setOrderBy] = useState("category");

  useEffect(() => {
    const fetchData = async () => {
      const result = await axios(
        `https://example.com/api/v1/news?orderBy=${orderBy}&order=${order}`
      );

      setData(result.data);
    };

    const interval = setInterval(() => {
      fetchData();
    }, 120000);
    return () => clearInterval(interval);
  }, []);

  const handleRequestSort = (event, property) => {
    const isAsc = orderBy === property && order === "asc";
    setOrder(isAsc ? "desc" : "asc");
    setOrderBy(property);
  };

The handleRequestSort does not seem to work.

<MaterialTable
          data={data}
          onRequestSort={handleRequestSort}
          columns={[
            {
              title: "Category",
              field: "category",
              render: (rowData) => {
                return <span>{rowData.category}</span>;
              },
            },
            {
              title: "Type",
              field: "type",
              render: (rowData) => {
                return <span>{rowData.type}</span>;
              },
            },
          ]}
        ></MaterialTable>

How can I make sure it is preserved?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!