What is little-state-machine?
The little-state-machine npm package is a lightweight state management library for React applications. It is designed to be simple and easy to use, making it ideal for small to medium-sized projects where you need to manage state without the overhead of more complex solutions.
What are little-state-machine's main functionalities?
State Initialization
This feature allows you to initialize the state of your application. The `createStore` function is used to create a store with an initial state.
import { createStore } from 'little-state-machine';
const initialState = {
user: {
name: '',
age: 0
}
};
const store = createStore(initialState);
State Update
This feature allows you to update the state. The `useStateMachine` hook is used to access actions that can update the state. In this example, the `updateUser` action updates the user information in the state.
import { useStateMachine } from 'little-state-machine';
const updateUser = (state, payload) => ({
...state,
user: {
...state.user,
...payload
}
});
const Component = () => {
const { actions } = useStateMachine({ updateUser });
const handleUpdate = () => {
actions.updateUser({ name: 'John', age: 30 });
};
return <button onClick={handleUpdate}>Update User</button>;
};
State Access
This feature allows you to access the current state. The `useStateMachine` hook is used to access the state, which can then be used in your components.
import { useStateMachine } from 'little-state-machine';
const Component = () => {
const { state } = useStateMachine();
return (
<div>
<p>Name: {state.user.name}</p>
<p>Age: {state.user.age}</p>
</div>
);
};
Other packages similar to little-state-machine
redux
Redux is a popular state management library for JavaScript applications. It is more complex and feature-rich compared to little-state-machine, making it suitable for larger applications with more complex state management needs.
mobx
MobX is a state management library that makes state management simple and scalable by transparently applying functional reactive programming. It is more flexible and powerful than little-state-machine, but also comes with a steeper learning curve.
zustand
Zustand is a small, fast, and scalable state management solution for React. It is similar to little-state-machine in terms of simplicity and ease of use, but offers more features and better performance.
Little State Machine
State management made super simple
✨ Features
- Tiny with 0 dependency and simple (less than 1.5kb)
- Persist state by default (
sessionStorage
or localStorage
) - Build with React Hooks
- Compatible with React Native
📦 Installation
$ npm install little-state-machine
Check out the
Demo.
🕹 API
🔗 StateMachineProvider
This is a Provider Component to wrapper around your entire app in order to create context.
🔗 createStore
createStore(store, options?: {
name: string;
middleWares?: Function[];
}})
Function to initialize the global store, invoked at your app root (where <StateMachineProvider />
lives).
import yourDetail from './state/yourDetail';
function log(store) {
console.log(store);
}
createStore(
{
yourDetail,
},
{
middleWares: [log],
},
);
🔗 useStateMachine
This hook function will return action/actions and state of the app.
import { updateUserNameAction, removeNameAction } from './actions/yourDetails';
const { action, state } = useStateMachine<T>(updateUserNameAction);
const { actions, state } = useStateMachine<T>({
removeNameAction,
updateUserNameAction,
});
const { action, state } = useStateMachine<T>(updateUserNameAction, {
shouldReRenderApp: false,
});
📖 Example
📋 app.js
import React from 'react';
import yourDetail from './yourDetail';
import YourComponent from './yourComponent';
import { StateMachineProvider, createStore } from 'little-state-machine';
import { DevTool } from 'little-state-machine-devtools';
createStore({
yourDetail,
});
export default () => {
return (
<StateMachineProvider>
<DevTool />
<YourComponent />
</StateMachineProvider>
);
};
📋 yourComponent.js
import React from 'react';
import { updateName } from './action.js';
import { useStateMachine } from 'little-state-machine';
export default function YourComponent() {
const {
action,
state: {
yourDetail: { name },
},
} = useStateMachine(updateName);
return <div onClick={() => action({ name: 'bill' })}>{name}</div>;
}
📋 yourDetail.js
export default {
name: 'test',
};
📋 action.js
export function updateName(state, payload) {
return {
...state,
yourDetail: {
...state.yourDetail,
...payload,
},
};
}
⚒ Little State Machine 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
For legacy IE11 support, you can import little-state-machine IE11 version.
import { createStore } from 'little-state-machine/dist/little-state-machine.ie11';
📋 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);
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.
Thank you very much for those kind people with their sponsorship to this project.