Little State Machine

React custom hook for persist state management

README

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

  1. ```tsx
  2. <StateMachineProvider>
  3.   <App />
  4. </StateMachineProvider>
  5. ```

🔗 createStore


Function to initialize the global store, invoked at your app root (where `` lives).

  1. ```tsx
  2. function log(store) {
  3.   console.log(store);
  4.   return store;
  5. }

  6. createStore(
  7.   {
  8.     yourDetail: { firstName: '', lastName: '' } // it's an object of your state
  9.   },
  10.   {
  11.      name?: string; // rename the store
  12.      middleWares?: [ log ]; // function to invoke each action
  13.      storageType?: Storage; // session/local storage (default to session)
  14.     
  15.      persist?: 'action' // onAction is default if not provided
  16.      // when 'none' is used then state is not persisted
  17.      // when 'action' is used then state is saved to the storage after store action is completed
  18.      // when 'beforeUnload' is used then state is saved to storage before page unloa
  19.   },
  20. );
  21. ```

🔗 useStateMachine


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

  1. ```tsx
  2. const { actions, state, getState } = useStateMachine<T>({
  3.   updateYourDetail,
  4. });
  5. ```

📖 Example


Check out the Demo.

  1. ```tsx
  2. import React from 'react';
  3. import {
  4.   StateMachineProvider,
  5.   createStore,
  6.   useStateMachine,
  7. } from 'little-state-machine';

  8. createStore({
  9.   yourDetail: { name: '' },
  10. });

  11. function updateName(state, payload) {
  12.   return {
  13.     ...state,
  14.     yourDetail: {
  15.       ...state.yourDetail,
  16.       ...payload,
  17.     },
  18.   };
  19. }

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

  22.   return (
  23.     <div onClick={() => actions.updateName({ name: 'bill' })}>
  24.       {state.yourDetail.name}
  25.     </div>
  26.   );
  27. }

  28. const App = () => (
  29.   <StateMachineProvider>
  30.     <YourComponent />
  31.   </StateMachineProvider>
  32. );
  33. ```

⌨️ Type Safety (TS)


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

Checkout the example.

  1. ```ts
  2. import 'little-state-machine';

  3. declare module 'little-state-machine' {
  4.   interface GlobalState {
  5.     yourDetail: {
  6.       name: string;
  7.     };
  8.   }
  9. }
  10. ```

💁‍♂️ Tutorial


Quick video tutorial on little state machine.

⚒ DevTool


DevTool component to track your state change and action.

  1. ```tsx
  2. import { DevTool } from 'little-state-machine-devtools';

  3. <StateMachineProvider>
  4.   <DevTool />
  5. </StateMachineProvider>;
  6. ```