Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class SomeComponent extends React.Component{
- state={
- current:100,
- history:[]
- }
- increment=(n)=>{
- this.setState({
- current:this.state.current+n,
- history: [...this.state.history , n]
- })
- }
- undo = () =>{
- const newHistory = [...this.state.history]
- const lastN = newHistory.pop()
- this.setState({
- current: this.state.current - lastN,
- history: newHistory
- })
- }
- render(){
- return (
- <div>
- <h1>{this.state.current}</h1>
- <button onClick={()=>this.increment(100)}>Up</button>
- <button onClick={()=>this.increment(-100)}>Down</button>
- <button onClick={this.undo}>Undo</button>
- </div>
- )
- }
- }
- ReactDOM.render(
- <SomeComponent />,
- document.getElementById("root")
- );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement