Created
October 27, 2019 07:13
-
-
Save noblesilence/85019c3cdba417d04608f7dfadcfb3dd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Component } from 'react'; | |
import { connect } from 'react-redux'; | |
import CounterControl from '../../components/CounterControl/CounterControl'; | |
import CounterOutput from '../../components/CounterOutput/CounterOutput'; | |
class Counter extends Component { | |
state = { | |
counter: 0 | |
} | |
counterChangedHandler = (action, value) => { | |
switch (action) { | |
case 'inc': | |
this.setState((prevState) => { return { counter: prevState.counter + 1 } }) | |
break; | |
case 'dec': | |
this.setState((prevState) => { return { counter: prevState.counter - 1 } }) | |
break; | |
case 'add': | |
this.setState((prevState) => { return { counter: prevState.counter + value } }) | |
break; | |
case 'sub': | |
this.setState((prevState) => { return { counter: prevState.counter - value } }) | |
break; | |
} | |
} | |
render() { | |
return ( | |
<div> | |
<CounterOutput value={this.props.ctr} /> | |
<CounterControl label="Increment" clicked={this.props.onIncrementCounter} /> | |
<CounterControl label="Decrement" clicked={this.props.onDecrementCounter} /> | |
<CounterControl label="Add 5" clicked={this.props.onAddCounter} /> | |
<CounterControl label="Subtract 5" clicked={this.props.onSubstractCounter} /> | |
</div> | |
); | |
} | |
} | |
const mapStateToProps = state => { | |
return { | |
ctr: state.counter | |
}; | |
}; | |
const mapDispatchToProps = dispatch => { | |
return { | |
onIncrementCounter: () => dispatch({ type: 'INCREMENT' }), | |
onDecrementCounter: () => dispatch({ type: 'DECREMENT' }), | |
onAddCounter: () => dispatch({ type: 'ADD' }), | |
onSubstractCounter: () => dispatch({ type: 'SUBSTRACT' }) | |
} | |
}; | |
export default connect(mapStateToProps, mapDispatchToProps)(Counter); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const initialState = { | |
counter: 0 | |
} | |
const reducer = (state = initialState, action) => { | |
if (action.type === 'INCREMENT') { | |
return { | |
counter: state.counter + 1 | |
} | |
} | |
if (action.type === 'DECREMENT') { | |
return { | |
counter: state.counter - 1 | |
} | |
} | |
if (action.type === 'ADD') { | |
return { | |
counter: state.counter + 5 | |
} | |
} | |
if (action.type === 'SUBSTRACT') { | |
return { | |
counter: state.counter - 5 | |
} | |
} | |
return state; | |
} | |
export default reducer; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment