项目作者: Ebazhanov

项目描述 :
React click counter example
高级语言: JavaScript
项目地址: git://github.com/Ebazhanov/click-counter.git
创建时间: 2020-04-16T14:13:04Z
项目社区:https://github.com/Ebazhanov/click-counter

开源协议:

下载


My example of click counter with using the State Hook

I styled my version with material-ui, you can try to reach 2 clicks and see the bonus ))

You might want to see from where it was all started, what is the simple implementation
  1. function App() {
  2. const [count, setCount] = useState(0);
  3. return (
  4. <>
  5. <div>{count}</div>
  6. <button
  7. type="button"
  8. onClick={() => setCount(count + 1)}
  9. >
  10. Count me!</button>
  11. </>
  12. );
  13. }
  14. ReactDOM.render(
  15. <App></App>,
  16. document.getElementById('root')
  17. );
  18. export default App;
How to run on http://localhost:3000:

We now have a counter that increases every time there’s a click on a Button.
This is a simple example to illustrate state. I don’t recommend you use it as a counter in a real application. Instead, you’d probably want to create this.state.history variable that stores exactly which button was clicked at which point as an array of the current state at different points in time.
Then, if you need a click count, you can access it with this.state.history.length, and that click count will be correct even if you undo past changes.