Skip to content

Instantly share code, notes, and snippets.

@syamjayaraj
Created December 7, 2018 05:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save syamjayaraj/5e979ac506e633fb4bb46610cb7ddb01 to your computer and use it in GitHub Desktop.
Save syamjayaraj/5e979ac506e633fb4bb46610cb7ddb01 to your computer and use it in GitHub Desktop.
import React, {Component} from 'react'
import Button from '../components/Button'
import {connect} from 'react-redux'
import {incrementCount, decrementCount} from '../actions/index'
class CounterComponent extends Component{
handleBtnActionIncrement = () => {
this.props.onIncrementClick(this.props.count)
}
handleBtnActionDecrement = () => {
this.props.onDecrementClick(this.props.count)
}
render() {
const {count}=this.props
return(
<div>
<h1>Count: {count}</h1>
<Button action={this.handleBtnActionIncrement} buttonTitle = "+" />
<Button action={this.handleBtnActionDecrement} buttonTitle = "-" />
</div>
)
}
}
const mapStateToProps = (state) => {
return {
count: state.counter.count
}
}
const mapDispatchToProps = (dispatch) => {
return {
onIncrementClick: (count) => {
dispatch(incrementCount(count))
},
onDecrementClick: (count) => {
if(count !== 0)
dispatch(decrementCount(count))
}
}
}
export default connect(mapStateToProps, mapDispatchToProps)(CounterComponent)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment