Skip to main content

14.10 Dispatching Actions From Within Component

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 {
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.counterChangedHandler("dec")}
/>
<CounterControl
label="Add 5"
clicked={() => this.counterChangedHandler("add", 5)}
/>
<CounterControl
label="Subtract 5"
clicked={() => this.counterChangedHandler("sub", 5)}
/>
</div>
);
}
}

const mapStateToProps = (state) => {
return {
ctr: state.counter,
};
};

const mapDispatchToProps = (dispatch) => {
return {
onIncrementCounter: () => dispatch({ type: "INCREMENT" }),
};
};
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,
};
}
return state;
};

export default reducer;