GithubHelp home page GithubHelp logo

redux-todo's Introduction

Redux Todo list

This is IMHO more readable and undersandable example than official. Some confusing parts like this todo constant has been removed. Basicaly, todo is helper function which looks like a reducer - but it is not. todo is a function which is passed to the todos reducer. Yeah, it gets state and action as parameters like reducer should - but still, todo constant is not a reducer.

Now, instead of having this:

const todo = (state, action) => {
  switch (action.type) {
    case 'ADD_TODO':
      return {
        id: action.id,
        text: action.text,
        completed: false
      }
    case 'TOGGLE_TODO':
      if (state.id !== action.id) {
        return state
      }

      return {
        ...state,
        completed: !state.completed
      }
    default:
      return state
  }
}

const todos = (state = [], action) => {
  switch (action.type) {
    case 'ADD_TODO':
      return [
        ...state,
        todo(undefined, action)
      ]
    case 'TOGGLE_TODO':
      return state.map(t =>
        todo(t, action)
      )
    default:
      return state
  }
}

export default todos

in src/reducers/todos.js we have just this:

const todos = (state = [], action) => {
  switch (action.type) {
    case 'ADD_TODO':
      return [
        ...state,
        {
          id: action.id,
          text: action.text,
          completed: false
        }
      ]
    case 'TOGGLE_TODO':
      return state.map(todo => 
        (todo.id !== action.id) 
          ? todo 
          : {...todo, completed: !todo.completed}
      )
    default:
      return state
  }
}
 
export default todos

Also getVisibleTodos in src/containers/VisibleTodoList.js is updated so instead of using switch statement, if conditional has been used.

const  getVisibleTodos = (todos, filter) => {
 if (filter === 'SHOW_ALL') {return todos} 
 if (filter === 'SHOW_COMPLETED') {return todos.filter(t => t.completed)} 
 if (filter === 'SHOW_ACTIVE') {return todos.filter(t => !t.completed)}
}

I have added initiall store in src/index.js:

const initiallStore = {
  visibilityFilter: 'SHOW_ALL',
  todos: [
    { 
      id: 1,
      text: 'First, learn React well',
      completed: true,
    },{ 
      id: 2,
      text: 'Consider using Redux',
      completed: true,
    },{
      id: 3,
      text: 'Keep all state in a single tree',
      completed: false
    }
  ]
}

and redux-logger so I can track reducers, actions and store more easily.

// src/index.js

// some code has been omitted

import { applyMiddleware, createStore } from 'redux';
import { createLogger } from 'redux-logger'

const logger = createLogger({ 
  collapsed: !false
});

const store = createStore(
  todoApp,
  initiallStore,
  applyMiddleware(logger)
)

redux-todo's People

Contributors

dankoknad avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.