All Projects → andrevenancio → snap-state

andrevenancio / snap-state

Licence: other
State management in a snap 👌

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to snap-state

React Workshop
⚒ 🚧 This is a workshop for learning how to build React Applications
Stars: ✭ 114 (+395.65%)
Mutual labels:  state-management, jsx, state
stoxy
Stoxy is a state management API for all modern Web Technologies
Stars: ✭ 73 (+217.39%)
Mutual labels:  state-management, preact, state
teaful
🍵 Tiny, easy and powerful React state management
Stars: ✭ 638 (+2673.91%)
Mutual labels:  state-management, preact, state
Freactal
Clean and robust state management for React and React-like libs.
Stars: ✭ 1,676 (+7186.96%)
Mutual labels:  state-management, preact, state
Hydux
A light-weight type-safe Elm-like alternative for Redux ecosystem, inspired by hyperapp and Elmish
Stars: ✭ 216 (+839.13%)
Mutual labels:  state-management, preact
React Easy State
Simple React state management. Made with ❤️ and ES6 Proxies.
Stars: ✭ 2,459 (+10591.3%)
Mutual labels:  state-management, state
Getx pattern
Design pattern designed to standardize your projects with GetX on Flutter.
Stars: ✭ 225 (+878.26%)
Mutual labels:  state-management, state
hyperapp-example
hyperapp example
Stars: ✭ 14 (-39.13%)
Mutual labels:  preact, state
Mobx Rest
REST conventions for Mobx
Stars: ✭ 164 (+613.04%)
Mutual labels:  state-management, state
Final Form
🏁 Framework agnostic, high performance, subscription-based form state management
Stars: ✭ 2,787 (+12017.39%)
Mutual labels:  state-management, state
react-globally
Gives you setGlobalState, so you can set state globally
Stars: ✭ 22 (-4.35%)
Mutual labels:  state-management, state
Statefulviewcontroller
Placeholder views based on content, loading, error or empty states
Stars: ✭ 2,139 (+9200%)
Mutual labels:  state-management, state
Hydrated bloc
An extension to the bloc state management library which automatically persists and restores bloc states.
Stars: ✭ 181 (+686.96%)
Mutual labels:  state-management, state
React Organism
Dead simple React state management to bring pure components alive
Stars: ✭ 219 (+852.17%)
Mutual labels:  state-management, state
State Machine Component
⚙️ State machine -powered components in 250 bytes
Stars: ✭ 178 (+673.91%)
Mutual labels:  state-management, preact
Xstate
State machines and statecharts for the modern web.
Stars: ✭ 18,300 (+79465.22%)
Mutual labels:  state-management, state
vuse-rx
Vue 3 + rxjs = ❤
Stars: ✭ 52 (+126.09%)
Mutual labels:  state-management, state
xoid
Framework-agnostic state management library designed for simplicity and scalability ⚛
Stars: ✭ 96 (+317.39%)
Mutual labels:  state-management, preact
XUI
XUI makes modular, testable architectures for SwiftUI apps a breeze!
Stars: ✭ 100 (+334.78%)
Mutual labels:  state-management, state
storken
🦩 Storken is a React State Manager. Simple as `useState`.
Stars: ✭ 22 (-4.35%)
Mutual labels:  state-management, state

snap-state

state management in a snap 👌. (under 1KB)

Motivation

You probably don't always need to use Redux or React Context API and wrapping your Consumer inside your Provider, or is it the other way around? 🤷‍♂️

For simpler applications you might just want a K.I.S.S. approach 🤔(did he just called me stupid?)

How does it work?

This library was done with React and Preact in mind, but if you're using your own thing good on you. I got you fam! Snap state is a little bit like a singleton with event dispatching. Everytime you set or change a property on the state, components listening to those events will be notified of changes.

So in the simplest form you can have

// 1) import the state
import { State } from 'snap-state';

// 2) define a property
State.test = 123;

This property State.test will now be available from anywhere in your application. You just need to import the State class. For example:

import { State } from 'snap-state';
console.log(State.test); // returns 123;

If you want to do anything every time the prop State.test changes, you can do something like:

import { State, onSnapState } from 'snap-state';

onSnapState(['test'], ({ key, value }) => {
    console.log('prop of key', key, 'changed to', value);
});

// change value of test to a random number.
State.test = Math.random();

// change value of test to a string
State.test = 'abc';

// change value of test to null
State.test = null;

The onSnapState method receives an array of props so you can listen to changes on multiple properties at the same time. It also returns a unsubscribe method that you should use when you want to unsubscribe from future events. The full example below:

import { State, onSnapState } from 'snap-state';

const unsubscribe = onSnapState(['test'], ({ key, value }) => {
    console.log('prop of key', key, 'changed to', value);
});

// change value of test to a random number
State.test = Math.random();

// stops the subscription to prop changes
unsubscribe();

// changing the prop now will work as expected, but the `onSnapState` callback isn't called because we unsubscribe from it.
State.test = 'abc';
State.test = null;

React

You can create your state somewhere (well, maybe at the application level to look profesh)! Once that's out of the way, you can change it from anywhere in your app regardless of component hierarchy. 🎉🎉🎉

Now every time the state changes, any component subscribing to changes will be updated.

For example, lets say we want to create a property that stores a "theme", how does that look? Well, just create your initial state:

import { State } from 'snap-state';

State.theme = 'dark';

Now you just need to subscribe to changes to that particular prop. How? However you want to! Some examples below.

Hooks

I know you're fancy, so if functional programming is your thing, check out the useSnapState hook.

import { useSnapState } from 'snap-state';

function Example() {
    const state = useSnapState(['theme']);
    return (
        <p>the theme is {state.theme}</p>
    );
}

Every time you change State.theme anywhere on your app, your component will be updated.

High Order Components

If you're more into class based components you can decorate your component with withSnapState HOC.

import React, { Component } from 'react';
import { withSnapState } from 'snap-state';

class Example extends Component {
    render() {
        return (
            <p>the theme is {this.props.theme}</p>
        );
    }
}

export default withSnapState(['theme'])(Example);

Vanilla

What if you're building a custom WebGL application that makes a cat fly through space? What if you want to store all the planets your Space Cat visits?

import { State, onSnapState } from 'snap-state';

// subscribe to changes on the "planet" prop
const unsubscribe = onSnapState(['planet'], ({ value }) => {
    console.log(`Space cat just went pass ${value}.`);
});

// change your state and look at that amazing callback.
State.planet = 'mars';

// when your cat reaches another galaxy and you feel its time to let it go
// you can unsubscribe from further changes
unsubscribe();
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].