i have quesetions in a Usestate demo

import "./styles.css";
import { useRef, useState } from "react";
export default function App() { const [count, setCount] = useState(0); const ref = useRef(null); const handleClick = () => { setCount(count + 1); setCount((pre) => pre + 1); }; return ( <div className="App"> <button onClick={handleClick}>+</button> <h1>{count}</h1> <h2>Start editing to see some magic happen!</h2> </div> );
} 
Enter fullscreen mode
Exit fullscreen mode

demo site as following. https://codesandbox.io/p/sandbox/affectionate-merkle-nrfkvh?file=%2Fsrc%2FApp.js%3A1%2C1-17%2C2

when i click the button, the count increased two at one time,but when i change code order in the handleClick function, seem like

const handleClick = () => { setCount((pre) => pre + 1); setCount(count + 1); };
Enter fullscreen mode
Exit fullscreen mode

the count increased one at one time.
why? i hope someone can help me to explain this,thank you


Discover more from Coursity

Subscribe to get the latest posts sent to your email.

Leave a Comment

Your email address will not be published. Required fields are marked *

Discover more from Coursity

Subscribe now to keep reading and get access to the full archive.

Continue reading

Scroll to Top
How to install docker and docker compose on ubuntu 20. State of the art datacenter get a host.