简体   繁体   中英

How to make same button to increase count and revert back to its previous state in React (useState)?

我想将计数更改为 1,并使相同的按钮在单击时恢复到以前的状态。

export const CountButton = () => {
  previousState = 100;
  const [count, setCount] = useState(false);
  return(
   <div>
     <button onClick={() => setCount(!count)}>Click Me!</button>
     <div>{count ? previousState + 1 : previousState }</div>
   </div>
  )
}

This code will increase the vote by one and then revert on the second click.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM