14.17 Outsourcing Action Types
New actions.js
actions.js
export const INCREMENT = "INCREMENT";
export const DECREMENT = "DECREMENT";
export const ADD = "ADD";
export const SUBTRACT = "SUBTRACT";
export const STORE_RESULT = "STORE_RESULT";
export const DELETE_RESULT = "DELETE_RESULT";
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";
import * as actionTypes from "../../store/actions";
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}
/>
<hr />
<button onClick={this.props.onStoreResult}>Store Result</button>
<ul>
{this.props.storedResults.map((strResult) => (
<li
key={strResult.id}
onClick={() => this.props.onDeleteResult(strResult.id)}
>
{strResult.value}
</li>
))}
</ul>
</div>
);
}
}
const mapStateToProps = (state) => {
return {
ctr: state.counter,
storedResults: state.results,
};
};
const mapDispatchToProps = (dispatch) => {
return {
onIncrementCounter: () => dispatch({ type: actionTypes.INCREMENT }),
onDecrementCounter: () => dispatch({ type: actionTypes.DECREMENT }),
onAddCounter: () => dispatch({ type: actionTypes.ADD, value: 5 }),
onSubtractCounter: () => dispatch({ type: actionTypes.SUBTRACT, value: 5 }),
onStoreResult: () => dispatch({ type: actionTypes.STORE_RESULT }),
onDeleteResult: (id) =>
dispatch({ type: actionTypes.DELETE_RESULT, resultElId: id }),
};
};
export default connect(mapStateToProps, mapDispatchToProps)(Counter);
reducer.js
reducer.js
import * as actionTypes from "./actions";
const initialState = {
counter: 0,
results: [],
};
const reducer = (state = initialState, action) => {
switch (action.type) {
case actionTypes.INCREMENT:
const newState = Object.assign({}, state);
newState.counter = state.counter + 1;
return newState;
case actionTypes.DECREMENT:
return {
...state,
counter: state.counter - 1,
};
case actionTypes.ADD:
return {
...state,
counter: state.counter + action.value,
};
case actionTypes.SUBTRACT:
return {
...state,
counter: state.counter - action.value,
};
case actionTypes.STORE_RESULT:
return {
...state,
results: state.results.concat({ id: new Date(), value: state.counter }),
};
case actionTypes.DELETE_RESULT:
// const id = 2;
// const newArray = [...state.results];
// newArray.splice(id, 1);
const updatedArray = state.results.filter(
(result) => result.id !== action.resultElId
);
return {
...state,
// results: newArray,
results: updatedArray,
};
default:
}
return state;
};
export default reducer;