New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

easy-state

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

easy-state

Simple state manipulation without any frameworks.

  • 0.2.0
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

EasyState

Simple state manipulation without any frameworks.

Build Status codecov

Getting started

First things first; import the module:

import { createStateTree } from 'easy-state';

To get you started, initialize a state tree with the function createStateTree.

const store = createStateTree({
  counter: 1,
});

To retrieve the current state at any point in time, use getState:

store.getState();

To alter any state from your state tree, use setState:

store.setState({
  counter: 2
});

Keep your UI in sync with your state by using subscribe:

store.subscribe(() => {
  myDOMElement.innerHTML = store.getState().counter
});

The beauty of the subscribe-method is that you only need to define your UI-rendering once, and not on every state change you want to do in your application.

For larger applications you can divide your stores into smaller pieces, to get more control over certain parts.

If you want to have more control over your applications state with multiple stores, you can use the function combineStores that easy-state provides.

import { createStateTree, combineStores } from 'easy-state';

const storeOne = createStateTree({ hello: 'world' });
const storeTwo = createStateTree({ foo: 'bar' });

// init rootStore and pass the other stores to it.
const store = combineStores({
  storeOne,
  storeTwo
});

// Then you can use it like this:
store.storeOne.getState();
store.storeOne.setState({ hello: 'something new' });

// and you can listen to the store you want:
store.storeTwo.subscribe(() => {
  // render some HTML here and it will only listen to storeTwo.
});

Motivation

I've often come across projects that needed to use plain jquery or vanilla JavaScript instead of any frameworks e.g React or VueJS, and there is one thing I've missed: Possibilty to have application state in sync with my UI without any hassle.

Inspiration

The library is inspired by both React and Redux. It's sort of a Redux-lib, without the reducers, action-creators and dispatching actions, but instead changing state with setState()-method like they do in React.

Keywords

FAQs

Package last updated on 13 Aug 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc