All Projects → jeffreyyoung → quick-redux

jeffreyyoung / quick-redux

Licence: other
helper functions to make redux and react development quicker

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to quick-redux

nstate
A simple but powerful react state management library with low mind burden
Stars: ✭ 11 (-81.97%)
Mutual labels:  state-management, immer
react-evoke
Straightforward action-driven state management for straightforward apps built with Suspense
Stars: ✭ 15 (-75.41%)
Mutual labels:  state-management, immer
react-immer
No nonsense state management with Immer and React hooks
Stars: ✭ 13 (-78.69%)
Mutual labels:  state-management, immer
rematch
The Redux Framework
Stars: ✭ 8,340 (+13572.13%)
Mutual labels:  state-management, immer
east-store
east-store is a state manager with easiest api that based hooks and immer.
Stars: ✭ 18 (-70.49%)
Mutual labels:  state-management, immer
Easy Peasy
Vegetarian friendly state for React
Stars: ✭ 4,525 (+7318.03%)
Mutual labels:  state-management, immer
vuse-rx
Vue 3 + rxjs = ❤
Stars: ✭ 52 (-14.75%)
Mutual labels:  state-management
ng-effects
Reactivity system for Angular. https://ngfx.io
Stars: ✭ 46 (-24.59%)
Mutual labels:  state-management
immer-adapter
🐹 Declarative state mutations
Stars: ✭ 47 (-22.95%)
Mutual labels:  immer
grand central
State-management and action-dispatching for Ruby apps
Stars: ✭ 20 (-67.21%)
Mutual labels:  state-management
reactive-states
Reactive state implementations (brainstorming)
Stars: ✭ 51 (-16.39%)
Mutual labels:  state-management
tuxi
✨ White glove service for your async needs
Stars: ✭ 14 (-77.05%)
Mutual labels:  state-management
vana
Observe your immutable state trees 🌲👀 (great with React)
Stars: ✭ 24 (-60.66%)
Mutual labels:  state-management
react-state
React-State - Superpowers for managing local and reusable state in React
Stars: ✭ 14 (-77.05%)
Mutual labels:  state-management
Accounting
A simple accounting app that provides basic additions, deletions, modifications, and provides a simple summary page, which is implemented by using MVI pattern.
Stars: ✭ 30 (-50.82%)
Mutual labels:  state-management
xoid
Framework-agnostic state management library designed for simplicity and scalability ⚛
Stars: ✭ 96 (+57.38%)
Mutual labels:  state-management
bloc
A predictable state management library that helps implement the BLoC design pattern
Stars: ✭ 12 (-80.33%)
Mutual labels:  state-management
stater
collection of Clojure/Script mount apps
Stars: ✭ 29 (-52.46%)
Mutual labels:  state-management
onli-reducer
⚛️ One line reducer. State management without boilerplate.
Stars: ✭ 31 (-49.18%)
Mutual labels:  state-management
screenmanager
Stackable Screen/State Management for the LÖVE framework.
Stars: ✭ 29 (-52.46%)
Mutual labels:  state-management

Build Status

quick-redux

Handle your state with modules. Rather than writing actions, and reducers, just write modules.

Getting started

npm install --save quick-redux redux react-redux

Simple example

1. create a module

const counterModule = {
  defaultState: {
    count: 0
  },
  actions: {
    increment: (state, num = 1) => state.count = state.count + num,
    decrement: (state, num = 1) => state.count = state.count - num,
  },
  selectors: {
    countWithActionsAndIsEven: (state, globalState, actions) => ({
      count: state.count,
      isEven: (state.count % 2) === 0,
      actions: actions.counter
    })
  },
  key: 'counter'
}

export default counterModule;

quick-redux uses immer to handle state modifications. it doesn't matter what an action returns, just modify the state passed into the action.

2. create a store from our modules

createStore takes our modules as arguments and returns

import ReactDOM from 'react-dom';
import {createStore} from 'quick-redux';
import { Provider } from 'react-redux'

//create a store
const store = createStore({
  counter: counterModule
});

//the rest is like a regular redux app
ReactDOM.render(
  <Provider store={store}>
    <App />
  </Provider>,
  document.getElementById('root')
);

3. access our store in a component

import React, { Component } from 'react';
import {inject} from 'quick-redux';

const enhance = inject(
  //path to your selector .ie moduleKey.selectorName
  'counter.countWithActionsAndIsEven'
);

const CounterComponent = ({count, isEven, actions}) => (
  <div>
    <h1>Count: {count}</h1>
    <h3>Is Even: {isEven}</h3>
    <button onClick={() => actions.increment()}>increment</button>
    <button onClick={() => actions.decrement()}>decrement</button>
    <button onClick={() => actions.increment(10000)}>INCREMENT BY 10,000!!!111!!!1</button>
  </div>
)

export default enhance(CounterComponent);

more complex example

this shows how to generate reducers using createReducers and action creators using getActions from our modules so that any redux middleware can be used with our store

import React from 'react';
import ReactDOM from 'react-dom';
import {createStore, combineReducers} from 'redux';
import {createReducers, getActions} from 'quick-redux';
import { Provider } from 'react-redux'

const todoModule = {
  defaultState: {
    todos: [],
    loading: false
  },
  actions: { //the action creators of these actions are prefixed with the module key, so they are scoped to the todoModule
    addTodo(state, todo) {
      state.todos.push(todo)
    },
    removeTodo(state, index) {
      state.todos = state.todos.splice(index, 1);
    },
    setLoading(state, loading) {
      state.loading = loading;
    }
  },
  globalActions: { //these actions are not prefixed, so if you call reset on any module, this action handler will be run
    reset(state) {
      state.todos = [];
      state.loading = false;
    }
  },
  asyncActions: {
    async loadTodos({actions, api}) {
      const todos = await api.loadTodos();
      todos.forEach(todo => actions.addTodo(todo));
    }
  },
  key: 'todoList'
};

const counterModule = {
  defaultState: {
    count: 0
  },
  actions: {
    increment: (state, num = 1) => state.count = state.count + num,
    decrement: (state, num = 1) => state.count = state.count - num,
  },
  globalActions: {
    reset: (state) => state.count = 0
  },
  key: 'counter'
};

const modules = {
  counter: counterModule,
  todoList: todoModule
}

const api = {
  loadTodos() {
    return new Promise((resolve) => {
      resolve([{id:1, text: 'finish something'}])
    });
  }
}

const reducers = createReducers(modules);
const store = createStore(combineReducers(reducers));

//anything passed into the third argument of get actions will all be passed into asyncAction handlers on any module
const actions = getActions(modules, store, {api});

async function run() {
  await actions.todos.loadTodos();
  actions.counter.increment(1000);
  console.log(store.getState());
  /*
    {
      counter: {
        count: 1000
      },
      todoList: {
        loading: false,
        todos: [{...}]
      }
    }
   */
  actions.todoList.reset();
  console.log(store.getState());
  /*
    {
      counter: {
        count: 0
      },
      todoList: {
        loading: false,
        todos: []
      }
    }
   */
}
run();

inpired by:

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].