A minimum and lightweight external store for React and React Native application

Overview

Reactive store - a minimum and lightweight external store for React and React Native application

Table of contents

  1. Features

  2. Installation

  3. Usage

    3.1 Create new store

    3.2 Inject store to React component

    3.3 Update data from store

    3.4 Memo data from store

    3.5 Middleware

  4. Quick start

  5. React 18 support

  6. About

Features

  • Inject external data to React hook component
  • Support action to remove or update data
  • Light weight
  • TypeScript support

Installation

yarn add @mcsheffey/reactive-store

or

npm install @mcsheffey/reactive-store

Usage

Create a store

import { Store } from '@mcsheffey/reactive-store';

const store = new Store();

//init data

store.add('count', 0);

Inject store to React Component

  • Using StoreInjector to make your store binding data with React component
import { StoreInjector } from '@mcsheffey/reactive-store';

const Component = () => {
  return <div></div>;
};
export const App = StoreInjector(store, Component);
//or
export const App = StoreInjector(store, () => {
  return <div></div>;
});
  • Many store can be inject into one Component, and two or more Component can use the same store
export const App = StoreInjector([countStore, todoStore], () => {
  return <div></div>;
});

Update or remove data from store

  • To update or remove data from store use can use store action. There are two kind of action update (update data) or remove (delete data) from store. Note: add new data to store will not trigger component rerender. This is good because data need to be declare at first time.
//this will not rerender component

store.add('blah', 'hmmm???');

//this two above will trigger component to rerender
store.update('count', count + 1);
store.remove('count');

Memo data

For some reason you may don't want to recreate data that extract from store every rerender, this can be achieved by using useCallback hook:

import * as React from 'react';
import { StoreInjector, Store } from '@mcsheffey/reactive-store';

const store = new Store();
store.add('count', 0);

const Component = () => {
  //Memo count 'state'
  const countMemo = React.useCallback(() => {
    return store.get('count');
  }, []);
  //Effect on memo
  React.useEffect(()=>{
     console.log("count change")
  },[countMemo()])
  return <p>{countMemo()}</p>;
};

export const App = StoreInjector(store, Component);

Middleware

  • Middleware is a method provide by Store class, it allow you to control, debugging store behavior

  • Using middleware:

import {
  DispatchAction,
  Store,
  StoreInjector,
} from '@mcsheffey/reactive-store';

export const store = new Store();

store.applyMiddleware(
  (key: string, current: any, payload: any, type: DispatchAction) => {
    console.log(key, current, payload, type);
  }
);

store.add('count', 0);
for (let i = 0; i < 2; i++) {
  const count = store.get('count');
  store.update('count', count++);
}
store.remove('count');
/*
Log:
"count" 0 undefined "add"
"count" 0 1 "update"
"count" 1 2 "update"
"count" 2 undefined "remove"
*/
  • key: the "key" of current data
  • current: current value of data with "key"
  • payload: a new value that use to update data with given key
  • type: an action type (add,update or remove)
  • You cant abort an action by return false in middleware function
store.applyMiddleware(
  (key: string, current: any, payload: any, type: DispatchAction) => {
    //abort action if count > 5
    if (count > 5) {
      return false;
    }
  }
);

store.add('count', 0);
for (let i = 0; i < 10; i++) {
  const count = store.get('count');
  store.update('count', count++);
}

Quick start

import {
  Store,
  StoreInjector,
  DispatchAction,
} from '@mcsheffey/reactive-store';

const store = new Store();

store.applyMiddleware(
  (key: string, current: any, payload: any, type: DispatchAction) => {
    console.log(key, current, payload, type);
    if (count > 5) {
      return false;
    }
  }
);

const keys = {
  count: 'count',
};
/*Add data to store*/
store.add(keys.count, 0);

const Component = () => {
  /*Get data from store */
  const count: number = store.get(keys.count);
  return (
    <div className="App">
      <button onClick={() => store.update(keys.count, count + 1)}>+</button>
    </div>
  );
};

const App = StoreInjector(store, Component);

const rootElement = document.getElementById('root');
ReactDOM.render(<App />, rootElement);

React 18 support

  • See document for React version 18 here

About

You might also like...
React-cursor-chat - A react component for cursor chat
React-cursor-chat - A react component for cursor chat

@yomo/react-cursor-chat 🧬 Introduction A react component for cursor chat Press

Maple.js is a React webcomponents based framework mixing ES6 with Custom Elements, HTML Imports and Shadow DOM. It has in-built support for SASS and JSX, including a Gulp task for vulcanizing your project.
Maple.js is a React webcomponents based framework mixing ES6 with Custom Elements, HTML Imports and Shadow DOM. It has in-built support for SASS and JSX, including a Gulp task for vulcanizing your project.

Heroku: http://maple-app.herokuapp.com/ npm: npm install maple.js Bower: bower install maple.js Maple is a seamless module that allows you to organise

CSS media queries in react - for responsive design, and more.

react-responsive Information Package react-responsive Description Media queries in react for responsive design Browser Version = IE6* Demo The best s

Configure and build views using JSON schemas mapped to React components

react-json-schema npm install react-json-schema Construct React elements from JSON by mapping JSON definitions to React components. Use react-json-sch

React UI Components for macOS High Sierra and Windows 10

React UI Components for macOS High Sierra and Windows 10. npm install react-desktop --save Help wanted! I am looking for developers to help me develop

:postbox: A simple and customizable React notifications system

Reapop A simple and customizable React notifications system Summary Compatibility Demo Installation Integration & usage With React & Redux With React

Useful components and utilities for working with React

react-extras Useful components and utilities for working with React Install $ npm install react-extras Usage import React from 'react'; import {If} f

The simple but very powerful and incredibly fast state management for React that is based on hooks

Hookstate The simple but very powerful and incredibly fast state management for React that is based on hooks. Why? • Docs / Samples • Demo application

A way to seamlessly integrate React and AngularJS

angulareact A way to seamlessly integrate React and AngularJS. Great for projects slowly migrating from AngularJS to React, supports using React compo

Releases(Middleware)
Owner
Hòa Võ
Hòa Võ
A reactive store that is fine, really. It works with React.

A reactive store that is fine, really. It works with React.

Steve Ruiz 109 Dec 17, 2022
Lightweight react-like library. Support for asynchronous rendering and hooks.

Recept · Lightweight react-like library. Like the name, this project is mainly based on the architectural idea of react, which can feel react more int

RuiLin Dong 52 Sep 17, 2022
A lightweight library to create reactive objects

Reactivity As the name says, Reactivity is a lightweight javascript library to create simple reactive objects. Inspired in Redux and Vuex Get started

David Linarez 2 Oct 28, 2021
A lightweight reactivity API for other UI libraries to be built on top of.

This is a tiny (~850B minzipped) library for creating reactive observables via functions. You can use observables to store state, create computed properties (y = mx + b), and subscribe to updates as its value changes.

Maverick 533 Jan 2, 2023
A performant, scalable and pluggable approach to instrumenting your React application.

react-i13n react-i13n provides a performant, scalable and pluggable approach to instrumenting your React application. Typically, you have to manually

Yahoo 369 Dec 25, 2022
React, React Native and Vue UI components for building data-driven apps with Elasticsearch

Reactive Search UI components library for Elasticsearch: Available for React, Vue and React Native Read how to build an e-commerce search UI a.) with

appbase.io 4.7k Jan 4, 2023
⚡️ Lightning-fast search for React and React Native applications, by Algolia.

React InstantSearch is a library for building blazing fast search-as-you-type search UIs with Algolia. React InstantSearch is a React library that let

Algolia 2k Dec 27, 2022
🤯 zART-stack — Zero-API, React [Native], & TypeScript

?? zART-stack — Zero-API, React [Native], & TypeScript

Alex Johansson 674 Jan 3, 2023
React ESI: Blazing-fast Server-Side Rendering for React and Next.js

React ESI: Blazing-fast Server-Side Rendering for React and Next.js React ESI is a super powerful cache library for vanilla React and Next.js applicat

Kévin Dunglas 633 Jan 5, 2023
react-pdf-highlighter is a React library that provides annotation experience for PDF documents on web.

☕️ Buy me a coffee react-pdf-highlighter react-pdf-highlighter is a React library that provides annotation experience for PDF documents on web. It is

Vladyslav 9 Dec 2, 2022