Coder Social home page Coder Social logo

therakeshpurohit / little-state-machine Goto Github PK

View Code? Open in Web Editor NEW

This project forked from beekai-oss/little-state-machine

0.0 0.0 0.0 2.44 MB

๐Ÿ“  React custom hook for persist state management

Home Page: https://lrz5wloklm.csb.app/

License: MIT License

JavaScript 5.83% TypeScript 94.17%

little-state-machine's Introduction

๐Ÿ“  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)
     
     persist?: 'action' // onAction is default if not provided
     // when 'none' is used then state is not persisted
     // when 'action' is used then state is saved to the storage after store action is completed
     // when 'beforeUnload' is used then state is saved to storage before page unloa
  },
);

๐Ÿ”— useStateMachine

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

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

๐Ÿ“– 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>
  );
}

const App = () => (
  <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;
    };
  }
}

๐Ÿ’โ€โ™‚๏ธ Tutorial

Quick video tutorial on little state machine.

โš’ DevTool

DevTool component to track your state change and action.

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

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

By the makers of BEEKAI

We also make BEEKAI. Build the next-generation forms with modern technology and best in class user experience and accessibility.

little-state-machine's People

Contributors

almusweb avatar bluebill1049 avatar ciganandrea avatar d1plo1d avatar dependabot[bot] avatar devonotbevo avatar dipiash avatar erickbelfy avatar flomocommon avatar jeffemery avatar jorisre avatar kotarella1110 avatar lookrain avatar mattl75 avatar monkeywithacupcake avatar neighborhood999 avatar snax4a avatar sshquack avatar ssuvorov 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.