Git Product home page Git Product logo

simple-shared-state's Introduction

Simple Shared State

Redux is verbose. SimpleSharedState is brief.

Status

SimpleSharedState is still relatively experimental. Versions are following semver, but the version being greater than 1.0 doesn't mean it's considered production ready just yet. Please review the project source and tests to determine if it's viable for your project. More elaborate tests are needed to test SimpleSharedState performance against Redux.

Get It

npm install simple-shared-state

If using script tags:

<script src="https://unpkg.com/simple-shared-state"></script>
<script>
  const store = new SimpleSharedState.Store({});
</script>

Support for Internet Explorer

Use dist/simple-shared-state.es5.umd.js. For example:

<script src="https://unpkg.com/simple-shared-state/dist/simple-shared-state.es5.umd.js"></script>

Basic Use

First create a store.

import { Store } from "simple-shared-state";

const initialState = {
  user: {
    name: "Alice",
    slogan: "simple-shared-state makes apps fun again",
  },
};
const actions = () => ({
  changeSlogan: (newSlogan) => ({
    user: {
      slogan: newSlogan,
    },
  }),
});
const store = new Store(initialState, actions);

Then create a watcher. Don't worry about error handling in selectors, just return the state that you want.

const selector = (state) => state.user;

store.watch(selector, (state) => {
  console.log("user snapshot:", state);
});

Then call your action to update state and trigger the watch handler.

store.actions.changeSlogan("simple-shared-state is better than cat memes");
// 'user snapshot:' { name: 'Alice', slogan: 'simple-shared-state is better than cat memes' }

Redux Devtools

Works with devtools, of course. Just pass in the reference like this:

const store = new Store(initialState, actions, window.__REDUX_DEVTOOLS_EXTENSION__);

React Hooks

useSimpleSharedState

Future Work

  • Fix redux devtools integration
  • Add support for async/await in action creators
  • Support typescript types
  • Explore potential to optimize selector processing for very large state trees

simple-shared-state's People

Watchers

 avatar  avatar

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.