jadjoubran / codetogo.io

🚀 JavaScript code to go - Find updated snippets for common JavaScript use cases
https://codetogo.io
MIT License
231 stars 30 forks source link

How to use state with hooks in React (edit to existing use case) #319

Closed luckyguy73 closed 4 months ago

luckyguy73 commented 1 year ago


function Counter() {
  const [count, setCount] = useState(0);

  function handleClick() {
    setCount(count + 1);
  }

  return (
    <>
      <h1>{count} times</h1>
      <button onClick={handleClick}>Add 1</button>
    </>
  );
}

// propose in the handleClick function
setCount(count => count + 1)
jadjoubran commented 4 months ago

Thanks, it's fixed 👍 https://codetogo.io/how-to-use-state-with-hooks-in-react/