Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: #32289

Open
wedothebest4you opened this issue Feb 1, 2025 · 0 comments
Open

Bug: #32289

wedothebest4you opened this issue Feb 1, 2025 · 0 comments
Labels
Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug

Comments

@wedothebest4you
Copy link

wedothebest4you commented Feb 1, 2025

Please see a code underneath. The expected and actual results are given below. Please help to understand the reason for the differences.

React version: React 18

Steps To Reproduce

Please run the sample code below.

import { useEffect, useState } from 'react';

export default function App() {
  const [counter, setCounter] = useState(null);

  useEffect(() => {
    let i = 0;
    setTimeout(() => {
      setCounter((c) => {
        console.log(c, i);
        return i;
      });
      i = i + 1;
      setTimeout(() => {
        setCounter((c) => {
          console.log(c, i);
          return i;
        });
        i = i + 1;
        setTimeout(() => {
          setCounter((c) => {
            console.log(c, i);
            return i;
          });
          i = i + 1;
        }, 500);
      }, 500);
    }, 500);
  }, []);
  return <>{counter && counter}</>;
}

The current behavior

In browser :

// 0/2/3

In console :

/*
    null,0 
    0,2 
    2,3
*/

The expected behavior

In browser :

// 1/2/3 (each digit delayed by half of a second)

In console :

/*

  null,1 
  1,2
  2,3
 
*/

@wedothebest4you wedothebest4you added the Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug label Feb 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug
Projects
None yet
Development

No branches or pull requests

1 participant