Last active
July 24, 2018 21:16
-
-
Save reccanti/387958e3f46885de773238a7b8188be6 to your computer and use it in GitHub Desktop.
Simple TODO Redux App
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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