Skip to content

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>

export default App


See also  Loop inside React JSX

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.