Giter VIP home page Giter VIP logo

simplestore's Introduction

simpleStore

Build Status Coverage Status FOSSA Status Maintainability

A simple state management for React.

Install

$ npm install skshetry/simplestore

Development

  1. npm install --dev to install dependencies
  2. npm run test
  3. npm run init to create a dist folder
  4. npm run build
  5. npm run clean to remove dist folder

Why

React(>=16.0) provides a context API that provides access to data without prop-drilling to the nested components. But, on large projects, to provide access to other part of the app that is not the child, Redux is used.

However for small usages, this may seem overkill.

simpleStore is basically a global object that can be used on small-to-medium scales. It provides simple API for accessing(get) and storing data(set) on the store. Any changes/updates to the data propagates to the connected components (connected using connect() which on later changes will call component's setState).

Creating store

Store(key[, callback])

A key has to be passed to the Store which will be used to set the state on the components later with the same key. At the first, the data is set undefined.

import Store from 'simplestore';

const fruitStore = Store('fruits');

// can even send a callback during creation
const toDoStore = Store('todos', () => {
    initialize_todos(); 
    // something, something here
})

Reading/Changing data

set(data) / get( )

get returns the copy of the data in the store.

set is async function that sets/overrides data and then returns new data after propagating new changes to the connected components. The data should not be mutated as doing this, the change will not propagate.

fruitStore.get() == undefined;
// true; undefined at first

fruitStore.set([
    'apple', 'mango', 'guava', 'litchi'
]);

fruitStore.set([
    'apples', 'mangoes'
]).then( data => {
    console.log('Fruits updated', data)
});

// ...  later
fruitStore.get(); //'apple', 'mango', 'guava', 'litchi'

Connecting to the Store

connect(component[, callback]) / disconnect(component)

connect should receive this(the React component itself) that will later be used to propagate the changes. On data set, setState of all the components are called with the data in store with the key used before on initialiation.

disconnect should also receive this that will disconnect it from any future changes to the store. It is an async function.

// ... Inside a react component
fruitStore.connect(this);

// can even receive a callback
fruitStore.connect(this, () => {
    console.log('I will now receive any',
                'changes made to the store.');
});

/// later, if you don't require it
fruitStore.disconnect(this);

/// with then()
fruitStore.disconnect(this).then(
    console.log('I won\'t be called now.');
)

Getting list of connections to a store

connections( )

Provides a list of connections to the store.

fruitStore.connections();

Get other stores

list([key])

Is a static function. If key is passed, this provides the instance having the key. If the store with the key doesn't exist, it will return undefined. If key isn't passed, this will return all the instances.

Store.list()

Store.list('fruits')
// fruitStore

Knowing key of the store for whatever reasons

key

fruitStore.key;
// 'fruits'

License

MIT

FOSSA Status

simplestore's People

Contributors

dependabot-preview[bot] avatar dependabot[bot] avatar fossabot avatar skshetry avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

fossabot

simplestore's Issues

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.