Update counter on click of button in React
In this example, the counter is updated every time the user clicks on the Update Counter button, the page is refreshed and the updated counter value is shown.
The refresh happens instantly without reloading all the resources, which is a nice user experience.
import { useState } from "react" function App() { const [counter, setCounter] = useState(0) return ( <div className="App"> <div>Count is {counter}</div> <button onClick={() => setCounter(counter + 1)}>Update Counter</button> </div> ) } export default App