Skip to content

Instantly share code, notes, and snippets.

@reccanti
Last active July 24, 2018 21:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reccanti/387958e3f46885de773238a7b8188be6 to your computer and use it in GitHub Desktop.
Save reccanti/387958e3f46885de773238a7b8188be6 to your computer and use it in GitHub Desktop.
Simple TODO Redux App
import React from "react";
import { createStore } from "redux";
// ACTIONS
const createIncrement = amount => ({
type: "INCREMENT",
amount
});
const createDecrement = amount => ({
type: "DECREMENT",
amount
});
// REDUCER
function counterReducer(state = { count: 0 }, action) {
switch (action.type) {
case "INCREMENT":
return { ...state, count: state.count + action.amount };
case "DECREMENT":
return { ...state, count: state.count - action.amount };
default:
return state;
}
}
// STORE
const store = createStore(counterReducer, { count: 0 });
window.store = store;
window.createDecrement = createDecrement;
window.createIncrement = createIncrement;
const Playground = () => null;
export default Playground;
import { createStore } from "redux";
// CREATE ACTIONS
const addTask = task => ({ type: "ADD_TASK", task });
const completeTask = task => ({ type: "COMPLETE_TASK", task });
// CREATE A REDUCER
function todos(state = [], action) {
switch (action.type) {
case "ADD_TASK":
return [...state, { task: action.task, completed: false }];
case "COMPLETE_TASK":
return state.map(
todo =>
todo.task === action.task
? { task: action.task, completed: true }
: todo
);
default:
return state;
}
}
// CREATE A STORE
const store = createStore(todos, []);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment