All Projects → vijayst → react-store

vijayst / react-store

Licence: GPL-3.0 license
Redux like store with React hooks and Context API

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to react-store

Redux Zero
A lightweight state container based on Redux
Stars: ✭ 1,977 (+12256.25%)
Mutual labels:  flux
Typescript Webpack React Redux Boilerplate
React and Redux with TypeScript
Stars: ✭ 182 (+1037.5%)
Mutual labels:  flux
Tinystate
A tiny, yet powerful state management library for Angular
Stars: ✭ 207 (+1193.75%)
Mutual labels:  flux
Retalk
🐤 The Simplest Redux
Stars: ✭ 168 (+950%)
Mutual labels:  flux
Reactor Addons
Official modules for the Reactor project
Stars: ✭ 175 (+993.75%)
Mutual labels:  flux
Metalhead.jl
Computer vision models for Flux
Stars: ✭ 191 (+1093.75%)
Mutual labels:  flux
Firedux
🔥 🐣 Firebase + Redux for ReactJS
Stars: ✭ 148 (+825%)
Mutual labels:  flux
Denormalizr
Denormalize data normalized with normalizr
Stars: ✭ 231 (+1343.75%)
Mutual labels:  flux
Multi Tenancy
Flux v1: Manage a multi-tenant cluster with Flux and Kustomize
Stars: ✭ 180 (+1025%)
Mutual labels:  flux
Flooks
🍸 A state manager for React Hooks
Stars: ✭ 201 (+1156.25%)
Mutual labels:  flux
General Store
Simple, flexible store implementation for Flux. #hubspot-open-source
Stars: ✭ 171 (+968.75%)
Mutual labels:  flux
Transformers.jl
Julia Implementation of Transformer models
Stars: ✭ 173 (+981.25%)
Mutual labels:  flux
K8s Gitops
GitOps principles to define kubernetes cluster state via code. Community around [email protected] is on discord: https://discord.gg/7PbmHRK
Stars: ✭ 192 (+1100%)
Mutual labels:  flux
Influxdb Client Python
InfluxDB 2.0 python client
Stars: ✭ 165 (+931.25%)
Mutual labels:  flux
Smitty
Tiny flux implementation built on mitt
Stars: ✭ 210 (+1212.5%)
Mutual labels:  flux
Fluxible
A pluggable container for universal flux applications.
Stars: ✭ 1,811 (+11218.75%)
Mutual labels:  flux
Bedrock
Build a Node web app with user authentication, security, and more in under 10 minutes. Now supports React Hot Loading for super-fast development. 👌
Stars: ✭ 187 (+1068.75%)
Mutual labels:  flux
FluxJS.jl
I heard you like compile times
Stars: ✭ 43 (+168.75%)
Mutual labels:  flux
Blazor Fluxor
DEPRECATED PROJECT. See FLUXOR
Stars: ✭ 216 (+1250%)
Mutual labels:  flux
Deox
Functional Type-safe Flux Standard Utilities
Stars: ✭ 200 (+1150%)
Mutual labels:  flux

react-reducer-store

Global state with React hooks and Context API

Demo app

Demo app uses the live version of react-reducer-store. To run the Demo app.

cd demo
yarn start

Demo app is hosted in Github pages. Apart from normal functionality that you expect from the app, clicking the Random better should not output the message: "List component is rendering" in the console. View the console and check if the logs are acceptable to you. The only middleware that is of real use if the redux-logger middleware. The rest of the middleware don't add value in the new Hooks api.

Usage

Combine reducers

Write as many reducers as you want in your React app. Combine all the reducers to a root reducer as follows.

import { combineReducers } from 'react-reducer-store';
import todoReducer from './todoReducer';
import randomReducer from './randomReducer';

export default combineReducers({
    todo: todoReducer,
    random: randomReducer
});

Use Store component

Wrap the root component of your project with Store component

import { Store } from 'react-reducer-store';
import rootReducer from './rootReducer';

export default function App() {
    return (
        <Store rootReducer={rootReducer}>
            <Form />
            <List />
        </Store>
    );
}

Use useDispatch hook

Use the useDispatch hook to get the dispatch function to root reducer.

import { useDispatch } from 'react-reducer-store';

export default function Form(props) {
    const dispatch = useDispatch()

    function handleRandom() {
        dispatch({ type: 'DO_RANDOM' });
    }

Use useStore hook

Use the store from the List component using useStore hook

import { useStore, useDispatch } from 'react-reducer-store';

export default function List() {
    const todo = useStore(state => state.todo, []);
    const dispatch = useDispatch();

    function handleDelete(id) {
        dispatch({
            type: 'DELETE_TODO',
            id
        });
    }

    return (
        <div className="list">
            {todo.map(item => (
                <ListItem key={item.id} 
                    onDelete={handleDelete.bind(null, item.id)} 
                    text={item.text} 
                />
            ))}
        </div>
    );
}

Logging

To add logging to the console, set the log prop on the Store to true.

<Store rootReducer={rootReducer} log={true}>
  ...
</Store>

Output looks like below:

Log Output

Challenges in doing this project with the new Hooks API

At first, I thought the useReducer is a great API, one which should have been part of React since the beginning. So, I started out creating a global store with just useReducer hook. I was hoping that calling useReducer with the same reducer function will give the same state. So, it was a great way of sharing state. But useReducer hook does not work that way. To more about useReducer hook, read my first blog post on this topic.

Then I thought of putting the global state in the Context. I was able to put the global state in context and use it everywhere using a useStore hook. I have outlined the approach in the second article on the same topic. There was a problem with this approach. There were too many updates happening when we put the global store in the context. All components subscribed to the store updated even when they don't need that data.

So, the official solution from React team is to put the dispatch function into the global context and pass the store down as props. I outlined this approach in the third article where I explain why Redux is required. At the time of writing the third article, I did not know the Redux team was struggling with the problem.

The problem of using subscription mechanism that I outlined in the third article to fix too many updates is that it does not work well with concurrent React. So, the only way to solve this problem is to pass the global store into the Context API. And figure out some way to cancel the update from happening. But there is no hook to cancel an update which is already kick-started by the useContext hook. So, the only way around this is to have a higher order component via the connect function to cancel the updates from the context. So, we are back to Square one where we have global store in Context object and a connect HOC to prevent too many updates. Currently, there is no way with the official React API to write a useStore hook. Till then, this is just an experimental project, definitely only for small apps and demo projects.

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].