Giter Site home page Giter Site logo

mahdikhashan / react-todo-usereducer Goto Github PK

View Code? Open in Web Editor NEW
2.0 1.0 0.0 864 KB

A simple todo implemented with useReducer hook

Home Page: https://spontaneous-sopapillas-b74396.netlify.app/

HTML 15.55% CSS 8.40% JavaScript 73.79% Dockerfile 2.26%
react use-reducer hook

react-todo-usereducer's Introduction

React useReducer hook

The intentions for this project was to implement a simple todo app using react useReducer hook.

How in the world to initialize a useReducer

a simple reducer hook expects us to provide a reducer object and initial state [] , here an empty array.

const [todos, dispatch] = useReducer(reducer, [])

so, easy-peasy, not? Then how to add value(s) to our empty state? right, we need to call dispatch function with our action and pass payload to it.

What's a dispatch function look like?

dispatch({ type: ACTIONS.ADD_TODO, payload: { name: item } })

so then, a dispatch function receives an object with action and payload.

Actions, like a command

we will define command in an enum like object, here it's called ACTIONS.

const ACTIONS = {
  ADD_TODO: 'add-todo',
  REMOVE_TODO: 'remove-todo',
  UPDATE_TODO: 'update-todo',
  DONE_TODO: 'done-todo'
}

Reducer

And finally, we will define our logic in a reducer method. Passing the state object, here todos, and commands action are arguments. Don't forget to set a default value if you used swtich-case logic.

const reducer = (todos, action) => {
  switch (action.type) {
    case ACTIONS.ADD_TODO:
      return [...todos, newTodo(action.payload.name)]
    case ACTIONS.REMOVE_TODO:
      // remove to-do from the state
      return todos.filter(todo => todo.id !== action.payload.id)
    case ACTIONS.UPDATE_TODO:
      // update to-do to the state
      return todos.map(todo => {
        if (todo.id === action.payload.id) {
          return { ...todo, name: action.payload.name }
        }
        return todo
      })
    case ACTIONS.DONE_TODO:
      return todos.map(todo => {
        if (todo.id === action.payload.id) {
          return { ...todo, complete: !todo.complete }
        }
        return todo
      })
    default:
      throw new Error();
  }
}

react-todo-usereducer's People

Contributors

mahdikhashan avatar

Stargazers

 avatar  avatar  avatar

Watchers

 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.