Security News
RubyGems.org Adds New Maintainer Role
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
little-state-machine
Advanced tools
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.
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>
);
};
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.
Flux state management made super simple
$ npm install little-state-machine
Check out the Demo.
StateMachineProvider
This is a Provider Component to wrapper around your entire app in order to create context.
createStore
Function to initial the global store, call at app root where StateMachineProvider
is.
useStateMachine(Action | Actions, Options) =>
{ action: (any) => void, actions: { [key: string] : (any) => void}, state: Object }
// individual action
Action: (store: Object, payload: any) => void;
// multiple actions
Actions: { [key: string] : Action }
// options to name action in debug, and weather trigger global state update to re-render entire app
Options: {
debugName: string, // unique debug name can really help you :)
shouldReRenderApp: boolean,
}
This hook function will return action/actions and state of the app.
Building in DevTool component.
<DevTool />
window.STATE_MACHINE_DEBUG
This will toggle the console output in dev tool.
window.LITTLE_STATE_MACHINE_DEBUG(true)
to turn debug on in console
window.LITTLE_STATE_MACHINE_DEBUG(false)
to turn off debug on in console
window.STATE_MACHINE_RESET
This will reset the entire store.
window.LITTLE_STATE_MACHINE_RESET()
to reset the localStorage or sessionStorage
window.STATE_MACHINE_GET_STORE
This will return the entire store.
window.STATE_MACHINE_GET_STORE()
window.STATE_MACHINE_SAVE_TO
Save into another session/local storage
window.STATE_MACHINE_GET_STORE(name: string)
window.STATE_MACHINE_LOAD
Load saved state into your app, you can either supply a name of your session/local storage, or supply a string of data.
window.STATE_MACHINE_GET_STORE({ storeName?: string, data?: Object })
storeName
: external session/local storage name
data
: string of data
š app.js
import React from 'react'
import yourDetail from './yourDetail'
import YourComponent from './yourComponent'
import { StateMachineProvider, createStore } from 'little-state-machine'
// create your store
createStore({
yourDetail,
});
export default () => {
return (
<StateMachineProvider>
<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('bill')}>{name}</div>
}
š yourDetail.js
export default {
name: 'test',
}
š action.js
export function updateName(state, payload) {
return {
...state,
yourDetail: {
name: payload
},
}
}
FAQs
State management made super simple
The npm package little-state-machine receives a total of 224,334 weekly downloads. As such, little-state-machine popularity was classified as popular.
We found that little-state-machine demonstrated a not healthy version release cadence and project activity because the last version was released 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.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.
Security News
Research
Socket's threat research team has detected five malicious npm packages targeting Roblox developers, deploying malware to steal credentials and personal data.