My Code Docs

My Code Docs

  • Docs
  • Projects
  • Components
  • Help

›Redux

Javascript

  • Promises and Asnyc/Await
  • Local Storage
  • Firebase
  • JS Language Basics
  • JS Array Functions
  • Keyboard and Mouse Input
  • ES6 Cheatsheet
  • ESLint Setup
  • Data Structures
  • Naming Conventions
  • Javascript Resources
  • Javascript Snippets
  • npm Module Creation

Node

  • Node JS Basics
  • Server Config and SSH

NextJS

  • NextJS Basics

JS/React Libs

  • Lodash Library
  • Axios
  • Ramda
  • Moment JS
  • Overmind
  • Redux Forms npm Module
  • React Beautiful DnD
  • Ant Design
  • Zustand State Managment

React

  • React Basics
  • React Hooks
  • React With Classes
  • Reach Router
  • React Router
  • React Components

Redux

  • Redux in React
  • My Redux Pattern
  • Redux with React (First Doc)
  • Simple Redux Package

ReactNative

  • React Native
  • React Navigation V5
  • React Navigation V4

HTMLCSS

  • HTML and CSS Snippets
  • Tailwind CSS

Electron

  • Electron React Boiler
  • Electron Basics
  • Electron Packaging
  • Electron Tooling

Simple Redux Package

This is a sample Redux implementation to help me understand the internals of how the real redux package works.

const ADD_TODO = 'ADD_TODO';
const REMOVE_TODO = 'REMOVE_TODO';
const TOGGLE_TODO = 'TOGGLE_TODO';
const ADD_GOAL = 'ADD_GOAL';
const REMOVE_GOAL = 'REMOVE_GOAL';

// Action Creators
function addToDo(todo) {
  return {
    type: ADD_TODO,
    todo
  };
}
function removeToDo(id) {
  return {
    type: REMOVE_TODO,
    id
  };
}
function toggleToDo(id) {
  return {
    type: TOGGLE_TODO,
    id
  };
}

function addGoal(goal) {
  return {
    type: ADD_GOAL,
    goal
  };
}
function removeGoal(id) {
  return {
    type: REMOVE_GOAL,
    id
  };
}

// App Reducer
function app (state = {}, action) {
  return {
    todos: todos(state.todos, action),
    goals: goals(state.goals, action)
  };
}

// Reducer
function todos (state = [], action) {
  switch(action.type) {
    case ADD_TODO:
      return [...state, action.todo];
    case REMOVE_TODO:
      return state.filter((todo) => todo.id !== action.id);
    case TOGGLE_TODO:
      return state.map((todo) => todo.id !== action.id ? todo : { ...todo, complete: !todo.complete })
    default:
      return state;
  }  
}

function goals (state =[], action) {
  switch (action.type) {
    case ADD_GOAL:
      return [...state, action.goal];
    case REMOVE_GOAL:
      return state.filter((goal) => goal.id !== action.id);
    default:
      return state;
  }
}



function createStore(reducer) {
  // Parts of the store
  // 1. The state
  // 2. Get the state
  // 3. Listen to changes in the state
  // 4. Update the state

  let state;
  let listeners = [];

  // 2. get the state
  const getState = () => state;
  // 3. listen
  const subscribe = (listener) => {
    listeners.push(listener);
    // Return an "unsubscribe" function
    // which just filters out the subscribed function
    return () => {
      listeners = listeners.filter((l) => l !== listener);
    }
  };

  // 4. update the state
  const dispatch = (action) => {
    // call our reducer(s)
    state = reducer(state, action);
    // loop over all listeners and invoke them
    listeners.forEach((listener) => listener())

  };

  return {
    getState,
    subscribe,
    dispatch,
  }
}

// create the store, passing in the main reducer
const store = createStore(app);

store.subscribe(() => console.log('CurrentState: ', store.getState()));

store.dispatch(addToDo(
  {
    id: 1,
    name: 'walk the dog',
  }
  ));

store.dispatch(addToDo({
    id: 2,
    name: 'Brain feeding',
  }
  ));

store.dispatch(toggleToDo(2))

store.dispatch(addGoal({id: 0, goal: 'Be Happy'}))

// // Actions
// {
//   type: ADD_TODO,
//   todo: {
//     id: 1,
//     name: 'walk the dog',
//   }
// }

// {
//   type: REMOVE_TODO,
//   id: 1
// }

// {
//   type: TOGGLE_TODO,
//   id: 1
// }
← Redux with React (First Doc)React Native →
My Code Docs
Docs
Getting Started (or other categories)Guides (or other categories)API Reference (or other categories)
Community
User ShowcaseStack OverflowProject ChatTwitter
More
BlogGitHubStar
Facebook Open Source
Copyright © 2020 McCoidCo