All Projects → bluebill1049 → Little State Machine

bluebill1049 / Little State Machine

Licence: mit
📠 React custom hook for persist state management

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to Little State Machine

tstate-machine
TypeScript implementation of State Manager(like StateMachine)
Stars: ✭ 20 (-96.94%)
Mutual labels:  state-management, state-machine, state
xstate
State machines and statecharts for the modern web.
Stars: ✭ 21,286 (+3154.74%)
Mutual labels:  state-management, state-machine, state
Xstate
State machines and statecharts for the modern web.
Stars: ✭ 18,300 (+2698.17%)
Mutual labels:  state-machine, state-management, state
xstate-viz
Visualizer for XState machines
Stars: ✭ 274 (-58.1%)
Mutual labels:  state-management, state-machine, state
vuex-but-for-react
A state management library for React, heavily inspired by vuex
Stars: ✭ 96 (-85.32%)
Mutual labels:  flux, state-management, state
mutable
State containers with dirty checking and more
Stars: ✭ 32 (-95.11%)
Mutual labels:  flux, state-management, state
temperjs
State management for React, made simple.
Stars: ✭ 15 (-97.71%)
Mutual labels:  flux, state-management, state
Machinery
State machine thin layer for structs (+ GUI for Phoenix apps)
Stars: ✭ 367 (-43.88%)
Mutual labels:  state-machine, state-management, state
RxReduxK
Micro-framework for Redux implemented in Kotlin
Stars: ✭ 65 (-90.06%)
Mutual labels:  flux, state-management, state
Reatom
State manager with a focus of all needs
Stars: ✭ 567 (-13.3%)
Mutual labels:  flux, state-management, state
Loona
🌕 Application State Management done with GraphQL
Stars: ✭ 270 (-58.72%)
Mutual labels:  flux, state-management
mini-kotlin
Minimal Flux architecture written in Kotlin.
Stars: ✭ 20 (-96.94%)
Mutual labels:  flux, state-management
zedux
⚡ A high-level, declarative, composable form of Redux https://bowheart.github.io/zedux/
Stars: ✭ 43 (-93.43%)
Mutual labels:  state-management, state-machine
alveron-old
Opinionated Elm-inspired Redux Component Architecture for React
Stars: ✭ 17 (-97.4%)
Mutual labels:  state-management, state
React Recomponent
🥫 Reason-style reducer components for React using ES6 classes.
Stars: ✭ 272 (-58.41%)
Mutual labels:  state-management, state
Govern
Component-based state management for JavaScript.
Stars: ✭ 270 (-58.72%)
Mutual labels:  state-management, state
Redux Orm
A small, simple and immutable ORM to manage relational data in your Redux store.
Stars: ✭ 2,922 (+346.79%)
Mutual labels:  state-management, state
Effector
The state manager ☄️
Stars: ✭ 3,572 (+446.18%)
Mutual labels:  state-management, state
Grox
Grox helps to maintain the state of Java / Android apps.
Stars: ✭ 336 (-48.62%)
Mutual labels:  flux, state-management
litestate
An ambitiously tiny, gizp ~800b, flux-like library to manage your state
Stars: ✭ 31 (-95.26%)
Mutual labels:  flux, state-management
Little State Machine - React Hooks for state management

Little State Machine

State management made super simple

npm downloads npm npm

✨ Features

  • Tiny with 0 dependency and simple (715B gzip)
  • Persist state by default (sessionStorage or localStorage)
  • Build with React Hooks

📦 Installation

$ npm install little-state-machine

🕹 API

🔗 StateMachineProvider

This is a Provider Component to wrapper around your entire app in order to create context.

<StateMachineProvider>
  <App />
</StateMachineProvider>

🔗 createStore

Function to initialize the global store, invoked at your app root (where <StateMachineProvider /> lives).

function log(store) {
  console.log(store);
  return store;
}

createStore(
  {
    yourDetail: { firstName: '', lastName: '' } // it's an object of your state
  },
  {
     name?: string; // rename the store
     middleWares?: [ log ]; // function to invoke each action
     storageType?: Storage; // session/local storage (default to session)
  },
);

🔗 useStateMachine

This hook function will return action/actions and state of the app.

const { actions, state } = useStateMachine<T>({
  updateYourDetail,
});

💁‍♂️ Tutorial

Quick video tutorial on little state machine.

📖 Example

Check out the Demo.

import React from 'react';
import {
  StateMachineProvider,
  createStore,
  useStateMachine,
} from 'little-state-machine';

createStore({
  yourDetail: { name: '' },
});

function updateName(state, payload) {
  return {
    ...state,
    yourDetail: {
      ...state.yourDetail,
      ...payload,
    },
  };
}

function YourComponent() {
  const { actions, state } = useStateMachine({ updateName });

  return (
    <div onClick={() => actions.updateName({ name: 'bill' })}>
      {state.yourDetail.name}
    </div>
  );
}

export default () => (
  <StateMachineProvider>
    <YourComponent />
  </StateMachineProvider>
);

⌨️ Type Safety (TS)

You can create a global.d.ts file to declare your GlobalState's type.

Checkout the example.

import 'little-state-machine';

declare module 'little-state-machine' {
  interface GlobalState {
    yourDetail: {
      name: string;
    };
  }
}
import * as React from 'react';
import {
  createStore,
  useStateMachine,
  StateMachineProvider,
  GlobalState,
} from 'little-state-machine';

createStore({
  yourDetail: { name: '' },
});

const updateName = (state: GlobalState, payload: { name: string }) => ({
  ...state,
  yourDetail: {
    ...state.yourDetail,
    ...payload,
  },
});

const YourComponent = () => {
  const { actions, state } = useStateMachine({
    updateName
  });

  return (
    <div onClick={() => actions.updateName({ name: 'Kotaro' })}>
      {state.yourDetail.name}
    </div>
  );
};

const App = () => (
  <StateMachineProvider>
    <YourComponent />
  </StateMachineProvider>
);

⚒ DevTool

DevTool component to track your state change and action.

import { DevTool } from 'little-state-machine-devtools';

<StateMachineProvider>
  <DevTool />
</StateMachineProvider>;

🖥 Browser Compatibility

Little State Machine supports all major browsers IE11 include !

If you run into issues, feel free to open an issue.

📋 Polyfill

Consider adding Object.entries() polyfill if you're wondering to have support for old browsers. You can weather consider adding snippet below into your code, ideally before your App.js file:

utils.[js|ts]

if (!Object.entries) {
  Object.entries = function (obj) {
    var ownProps = Object.keys(obj),
      i = ownProps.length,
      resArray = new Array(i); // preallocate the Array
    while (i--) resArray[i] = [ownProps[i], obj[ownProps[i]]];
    return resArray;
  };
}

Or you can add core-js polyfill into your project and add core-js/es/object/entries in your polyfills.[js|ts] file.

Sponsors

Thank you very much for those kind people with their sponsorship to this project.

@sayav @lemcii @washingtonsoares @lixunn @SamSamskies @peaonunes @wilhelmeek @iwarner @joejknowles @chris-gunawardena @Tymek @Luchanso @vcarel @gragland @tjshipe @krnlde @msutkowski @mlukaszczyk

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].