Package Exports
- little-state-machine
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (little-state-machine) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
Little State Machine
Flux state management should be easy
- Follow flux application architecture
- Tiny with 0 dependency and simple
- Persist your state by default
- Build with React Hook
Install
$ npm install little-state-machineDemo
Check out the Demo.
API
// 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,
}
🔗 useStateMachine(Action | Actions, Options) =>
{ action: (any) => void, actions: { [key: string] : (any) => void}, state: Object }
This hook function will return action/actions and state of the app.
🔗 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
🔗 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.
Example
app.js
import React, { useState } from 'react'
import yourDetail from './yourDetail'
import YourComponent from './yourComponent'
import { StateMachineProvider, createStore } from 'little-state-machine'
// create your store
createStore({
yourDetail,
});
const App = ({children}) => {
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: { 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,
name: payload,
}
}