2018-10-04 14:37:37 +01:00
---
id: 5a24c314108439a4d4036177
title: Write a Simple Counter
challengeType: 6
2019-08-05 09:17:33 -07:00
forumTopicId: 301425
2021-01-13 03:31:00 +01:00
dashedName: write-a-simple-counter
2018-10-04 14:37:37 +01:00
---
2020-11-27 19:02:05 +01:00
# --description--
2018-10-04 14:37:37 +01:00
2020-11-27 19:02:05 +01:00
You can design a more complex stateful component by combining the concepts covered so far. These include initializing `state` , writing methods that set `state` , and assigning click handlers to trigger these methods.
# --instructions--
The `Counter` component keeps track of a `count` value in `state` . There are two buttons which call methods `increment()` and `decrement()` . Write these methods so the counter value is incremented or decremented by 1 when the appropriate button is clicked. Also, create a `reset()` method so when the reset button is clicked, the count is set to 0.
**Note:** Make sure you don't modify the `classNames` of the buttons. Also, remember to add the necessary bindings for the newly-created methods in the constructor.
# --hints--
`Counter` should return a `div` element which contains three buttons with text content in this order `Increment!` , `Decrement!` , `Reset` .
```js
assert(
(() => {
const mockedComponent = Enzyme.mount(React.createElement(Counter));
return (
mockedComponent.find('.inc').text() === 'Increment!' & &
mockedComponent.find('.dec').text() === 'Decrement!' & &
mockedComponent.find('.reset').text() === 'Reset'
);
})()
);
```
The state of `Counter` should initialize with a `count` property set to `0` .
```js
const mockedComponent = Enzyme.mount(React.createElement(Counter));
assert(mockedComponent.find('h1').text() === 'Current Count: 0');
```
Clicking the increment button should increment the count by `1` .
```js
const mockedComponent = Enzyme.mount(React.createElement(Counter));
mockedComponent.find('.inc').simulate('click');
assert(mockedComponent.find('h1').text() === 'Current Count: 1');
2018-10-04 14:37:37 +01:00
```
2020-11-27 19:02:05 +01:00
Clicking the decrement button should decrement the count by `1` .
2018-10-04 14:37:37 +01:00
2020-11-27 19:02:05 +01:00
```js
const mockedComponent = Enzyme.mount(React.createElement(Counter));
mockedComponent.find('.dec').simulate('click');
assert(mockedComponent.find('h1').text() === 'Current Count: -1');
```
Clicking the reset button should reset the count to `0` .
```js
const mockedComponent = Enzyme.mount(React.createElement(Counter));
mockedComponent.setState({ count: 5 });
const currentCountElement = mockedComponent.find('h1');
assert(currentCountElement.text() === 'Current Count: 5');
mockedComponent.find('.reset').simulate('click');
assert(currentCountElement.text() === 'Current Count: 0');
```
2018-10-04 14:37:37 +01:00
2020-11-27 19:02:05 +01:00
# --seed--
## --after-user-code--
```jsx
ReactDOM.render(< Counter / > , document.getElementById('root'))
```
## --seed-contents--
2018-10-04 14:37:37 +01:00
```jsx
class Counter extends React.Component {
constructor(props) {
super(props);
this.state = {
count: 0
};
2020-09-15 09:53:25 -07:00
// Change code below this line
2018-10-04 14:37:37 +01:00
2020-09-15 09:53:25 -07:00
// Change code above this line
2018-10-04 14:37:37 +01:00
}
2020-09-15 09:53:25 -07:00
// Change code below this line
2018-10-04 14:37:37 +01:00
2020-09-15 09:53:25 -07:00
// Change code above this line
2018-10-04 14:37:37 +01:00
render() {
return (
< div >
< button className = 'inc' onClick = {this.increment} > Increment!< / button >
< button className = 'dec' onClick = {this.decrement} > Decrement!< / button >
< button className = 'reset' onClick = {this.reset} > Reset< / button >
< h1 > Current Count: {this.state.count}< / h1 >
< / div >
);
}
};
```
2020-11-27 19:02:05 +01:00
# --solutions--
2018-10-04 14:37:37 +01:00
2020-07-13 18:58:50 +02:00
```jsx
2018-10-04 14:37:37 +01:00
class Counter extends React.Component {
constructor(props) {
super(props);
this.state = {
count: 0
};
this.increment = this.increment.bind(this);
this.decrement = this.decrement.bind(this);
this.reset = this.reset.bind(this);
}
reset() {
this.setState({
count: 0
});
}
increment() {
2019-09-24 16:12:50 +01:00
this.setState(state => ({
count: state.count + 1
}));
2018-10-04 14:37:37 +01:00
}
decrement() {
2019-09-24 16:12:50 +01:00
this.setState(state => ({
count: state.count - 1
}));
2018-10-04 14:37:37 +01:00
}
render() {
return (
< div >
< button className = 'inc' onClick = {this.increment} > Increment!< / button >
< button className = 'dec' onClick = {this.decrement} > Decrement!< / button >
< button className = 'reset' onClick = {this.reset} > Reset< / button >
< h1 > Current Count: {this.state.count}< / h1 >
< / div >
);
}
};
```