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

146
Views
Out of sync state and view

Facing a strange issue with the latest stable releases of Nextjs and React, where state and view updates are out-of-sync.

Example (v1): using useMemo

import { useState, useMemo } from "react";

const items = [
  { id: 1, name: "apple" },
  { id: 2, name: "orange" },
  { id: 3, name: "mango" }
];

export default function IndexPage() {
  const [desc, setDesc] = useState(true);

  const sortedItems = useMemo(() => [...(desc ? items : items.reverse())], [
    desc
  ]);

  return (
    <div>
      <button onClick={() => setDesc((s) => !s)}>change order</button>
      <br />
      <span>desc: {desc.toString()}</span>
      <pre>{JSON.stringify(sortedItems, null, 2)}</pre>
    </div>
  );
}

Example (v2): using useState

import { useState, useMemo } from "react";

const items = [
  { id: 1, name: "apple" },
  { id: 2, name: "orange" },
  { id: 3, name: "mango" }
];

export default function IndexPage() {
  const [state, setState] = useState({ desc: true, items });

  function handleSort() {
    setState((s) =>
      s.desc
        ? { desc: false, items: [...items.reverse()] }
        : { desc: true, items: [...items] }
    );
  }

  return (
    <div>
      <button onClick={handleSort}>change order</button>
      <br />
      <span>desc: {state.desc.toString()}</span>
      <pre>{JSON.stringify(state.items, null, 2)}</pre>
    </div>
  );
}

Package versions:

"next": "12.1.6",
"react": "18.2.0",
"react-dom": "18.2.0",

As you can see from the output below, array elements are not updated on the view front based on the current sort state (in both the examples). Multiple clicks are required to do so.

Is this a random bug or am I fooling myself!?

CodeSandbox - Contains both code examples

Output

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

From the useState example, separating the states for checking if it has been sorted and state for holding the items would be a great start.

import { useState } from "react";

const items = [
  { id: 1, name: "apple" },
  { id: 2, name: "orange" },
  { id: 3, name: "mango" },
];

export default function IndexPage() {
  const [areSorted, setAreSorted] = useState(false);
  const [itemsArr, setItemsArr] = useState(items);

  function handleSort() {
    setAreSorted(!areSorted);
    setItemsArr(itemsArr.reverse());
  }

  return (
    <div>
      <button onClick={handleSort}>change order</button>
      <br />
      <span>{"Desc : " + areSorted}</span>
      <pre>{JSON.stringify(itemsArr, null, 2)}</pre>
    </div>
  );
}

about 4 years ago · Juan Pablo Isaza Report
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!