Advertisement
WillmanCodes

Untitled

Oct 25th, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.92 KB | None | 0 0
  1. class SomeComponent extends React.Component{
  2. state={
  3. current:100,
  4. history:[]
  5. }
  6.  
  7. increment=(n)=>{
  8. this.setState({
  9. current:this.state.current+n,
  10. history: [...this.state.history , n]
  11. })
  12. }
  13.  
  14. undo = () =>{
  15.  
  16. const newHistory = [...this.state.history]
  17. const lastN = newHistory.pop()
  18. this.setState({
  19. current: this.state.current - lastN,
  20. history: newHistory
  21. })
  22.  
  23. }
  24. render(){
  25. return (
  26. <div>
  27. <h1>{this.state.current}</h1>
  28. <button onClick={()=>this.increment(100)}>Up</button>
  29. <button onClick={()=>this.increment(-100)}>Down</button>
  30. <button onClick={this.undo}>Undo</button>
  31. </div>
  32. )
  33. }
  34. }
  35.  
  36. ReactDOM.render(
  37. <SomeComponent />,
  38. document.getElementById("root")
  39. );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement