|
| 1 | +import React, { Component } from 'react'; |
| 2 | + |
| 3 | +/** |
| 4 | + * 🏆 |
| 5 | + * Here we have a Counter component that display the current value of the counter |
| 6 | + * It also has two buttons to increase ('+') or decrease('-') the counter. |
| 7 | + * The counter value will be stored in the state. |
| 8 | + * You need to update the state to add 1 to the counter when |
| 9 | + * "+" is clicked and substract 1 to the current when "-" is clicked |
| 10 | + */ |
| 11 | +class Counter extends Component<any, any> { |
| 12 | + constructor(props){ |
| 13 | + super(props); |
| 14 | + /** |
| 15 | + * ✏️ |
| 16 | + * Initialize a state here with initial value of counter set to 0 |
| 17 | + * this.state = { counter: defaultValue } |
| 18 | + */ |
| 19 | + this.state = {}; |
| 20 | + |
| 21 | + /** |
| 22 | + * 💡 |
| 23 | + * We are binding the methods here, don't worry about this right now |
| 24 | + * We will look at why we do this later in the tutorial |
| 25 | + */ |
| 26 | + this.increment = this.increment.bind(this); |
| 27 | + this.decrement = this.decrement.bind(this); |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + *💡 |
| 32 | + * This method will be called when the user clicks "+" button to increase the counter |
| 33 | + */ |
| 34 | + increment(){ |
| 35 | + /** |
| 36 | + * ✏️ |
| 37 | + * You need to call setState here to update the `counter` state |
| 38 | + * When user clicks the "+" we need to add 1 to the current state and |
| 39 | + * set the state with the new value. |
| 40 | + * We need to use value of current state to derive the new state, |
| 41 | + * so it's better to use the updater function like |
| 42 | + * this.setState(function(currentState) { |
| 43 | + * return newState |
| 44 | + * }); |
| 45 | + */ |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + *💡 |
| 50 | + * This method will be called when the user clicks "-" button to decrease the counter |
| 51 | + */ |
| 52 | + decrement(){ |
| 53 | + /** |
| 54 | + * ✏️ |
| 55 | + * You need to call setState here to update the `counter` state |
| 56 | + * When user clicks the "-" we need to subtract 1 to the current state and |
| 57 | + * set the state with the new value. |
| 58 | + * We need to use value of current state to derive the new state, |
| 59 | + * so it's better for us to use the updater function like |
| 60 | + * this.setState(function(currentState) { |
| 61 | + * return newState |
| 62 | + * }); |
| 63 | + */ |
| 64 | + } |
| 65 | + |
| 66 | + render() { |
| 67 | + return ( |
| 68 | + <div style={style.container}> |
| 69 | + <div style={style.buttons} |
| 70 | + onClick={this.decrement}> |
| 71 | + - |
| 72 | + </div> |
| 73 | + <div style={style.counter}> |
| 74 | + {this.state.counter} |
| 75 | + </div> |
| 76 | + <div style={style.buttons} |
| 77 | + onClick={this.increment}> |
| 78 | + + |
| 79 | + </div> |
| 80 | + </div> |
| 81 | + ); |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +/** |
| 86 | + * 💡 |
| 87 | + * This is just some styling used |
| 88 | + * You don't need to worry about this or change this |
| 89 | + */ |
| 90 | +const style = { |
| 91 | + container: { |
| 92 | + display: 'flex' |
| 93 | + }, |
| 94 | + buttons: { |
| 95 | + padding: `0px 7px 0px 7px`, |
| 96 | + backgroundColor: 'grey', |
| 97 | + cursor: 'pointer' |
| 98 | + }, |
| 99 | + counter: { |
| 100 | + padding: `0px 7px 0px 7px` |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | + |
| 105 | +/** |
| 106 | + * 🚨 🚨 DO NOT DELETE OR CHANGE THIS.🚨 🚨 |
| 107 | + * This is how you would use your above component and |
| 108 | + * the output of this code is displayed on the browser |
| 109 | + */ |
| 110 | +const Usage = (props) => { |
| 111 | + return <Counter /> |
| 112 | +}; |
| 113 | + |
| 114 | +export default Usage; |
0 commit comments