OK, so our Redux implementation seems to be working. It's time to clean it up a bit. We need to move the reducer out into its own file, like so:
// dataflow/reducer.js
function itemsReducer(state = [], action) {
switch(action.type) {
case "CREATE_ITEM":
return [...state, Object.assign(action.payload) ];
default:
return state;
}
}
It's also a good idea to add a select() function to the store as we sometimes don't want to move a full state back, only part of it. Our list view will benefit from the use of the select() function. Let's add that next:
// dataflow/redux-stepII.js
// this now refers to the reducers.js file we broke out
import { itemsReducer } from "./reducers";
let state = {
items: []
};
function store(state = { items: [] }, action) {
return {
items: itemsReducer(state.items, action)
};
}
export function getState() {
return state;
}
export function dispatch(action) {
state = store(state, action);
}
export function select(slice) {
return state[slice];
}