2018-12-17 16:34:32 +00:00
|
|
|
import React, { Component } from 'react'
|
2018-05-07 13:03:54 +00:00
|
|
|
import { connect } from 'react-redux'
|
2018-05-15 07:49:07 +00:00
|
|
|
import { incrementCount, decrementCount, resetCount } from '../store'
|
2018-05-07 13:03:54 +00:00
|
|
|
|
2018-05-15 07:49:07 +00:00
|
|
|
class Counter extends Component {
|
|
|
|
increment = () => {
|
2018-12-17 16:34:32 +00:00
|
|
|
const { dispatch } = this.props
|
2018-05-15 07:49:07 +00:00
|
|
|
dispatch(incrementCount())
|
|
|
|
}
|
|
|
|
|
|
|
|
decrement = () => {
|
2018-12-17 16:34:32 +00:00
|
|
|
const { dispatch } = this.props
|
2018-05-15 07:49:07 +00:00
|
|
|
dispatch(decrementCount())
|
|
|
|
}
|
|
|
|
|
|
|
|
reset = () => {
|
2018-12-17 16:34:32 +00:00
|
|
|
const { dispatch } = this.props
|
2018-05-15 07:49:07 +00:00
|
|
|
dispatch(resetCount())
|
2018-05-07 13:03:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
render () {
|
|
|
|
const { count } = this.props
|
|
|
|
return (
|
|
|
|
<div>
|
2018-12-17 16:34:32 +00:00
|
|
|
<h1>
|
|
|
|
Count: <span>{count}</span>
|
|
|
|
</h1>
|
2018-05-15 07:49:07 +00:00
|
|
|
<button onClick={this.increment}>+1</button>
|
|
|
|
<button onClick={this.decrement}>-1</button>
|
|
|
|
<button onClick={this.reset}>Reset</button>
|
2018-05-07 13:03:54 +00:00
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function mapStateToProps (state) {
|
2018-12-17 16:34:32 +00:00
|
|
|
const { count } = state
|
|
|
|
return { count }
|
2018-05-07 13:03:54 +00:00
|
|
|
}
|
|
|
|
|
2018-05-15 07:49:07 +00:00
|
|
|
export default connect(mapStateToProps)(Counter)
|