14.11 Practice
Counter.js
Counter.js
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 {
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.onSubtractCounter}
/>
</div>
);
}
}
const mapStateToProps = (state) => {
return {
ctr: state.counter,
};
};
const mapDispatchToProps = (dispatch) => {
return {
onIncrementCounter: () => dispatch({ type: "INCREMENT" }),
onDecrementCounter: () => dispatch({ type: "DECREMENT" }),
onAddCounter: () => dispatch({ type: "ADD", value: 5 }),
onSubtractCounter: () => dispatch({ type: "SUBTRACT", value: 5 }),
};
};
export default connect(mapStateToProps, mapDispatchToProps)(Counter);
reducer.js
reducer.js
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 + action.value,
};
}
if (action.type === "SUBTRACT") {
return {
counter: state.counter - action.value,
};
}
return state;
};
export default reducer;