
Product
Announcing Bun and vlt Support in Socket
Bringing supply chain security to the next generation of JavaScript package managers
little-state-machine
Advanced tools
State management made super simple
sessionStorage or localStorage)$ npm install little-state-machine
createStoreFunction to initialize the global store.
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
},
);
useStateMachineThis hook function will return action/actions and the state of the app.
const { actions, state, getState } = useStateMachine<T>({
actions?: Record<string, Function> // Optional action to update global state
selector?: Function, // Optional selector to isolate re-render based on selected state
});
Check out the Demo.
import { createStore, useStateMachine } from 'little-state-machine';
createStore({
yourDetail: { name: '' },
});
function updateName(state, payload) {
return {
...state,
yourDetail: {
...state.yourDetail,
...payload,
},
};
}
function selector(state) {
return state.yourDetails.name.length > 10;
}
function YourComponent() {
const { actions, state } = useStateMachine({ actions: { updateName } });
return (
<buttton onClick={() => actions.updateName({ name: 'bill' })}>
{state.yourDetail.name}
</buttton>
);
}
function YourComponentSelectorRender() {
const { state } = useStateMachine({ selector });
return <p>{state.yourDetail.name]</p>;
}
const App = () => (
<>
<YourComponent />
<YourComponentSelectorRender />
</>
);
You can create a global.d.ts file to declare your GlobalState's type.
Check out the example.
import 'little-state-machine';
declare module 'little-state-machine' {
interface GlobalState {
yourDetail: {
name: string;
};
}
}
StateMachineProvider has been removed, simple APIconst App = () => (
- <StateMachineProvider>
<YourComponent />
- <StateMachineProvider>
);
useStateMachine({ actions: { updateName } })We also make BEEKAI. Build the next-generation forms with modern technology and best in class user experience and accessibility.
Thanks go to these wonderful people:
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 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 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.
FAQs
State management made super simple
We found that little-state-machine demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Product
Bringing supply chain security to the next generation of JavaScript package managers

Product
A safer, faster way to eliminate vulnerabilities without updating dependencies

Product
Reachability analysis for Ruby is now in beta, helping teams identify which vulnerabilities are truly exploitable in their applications.