Check out the Demo.
@@ -30,9 +31,11 @@ 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; // rename the store
@@ -53,34 +56,39 @@ function log(store) {
console.log(store);
}
-createStore({
- yourDetail, // it's an object of your state { firstName: '', lastName: '' }
-}, {
- middleWares: [log], // an array of middleWares, which gets run each actions
- syncStores: { // you can sync with external store and transform the data
- externalStoreName: 'externalStoreName',
- transform: ({ externalStoreData, currentStoreData }) => {
- return { ...externalStoreData, ...currentStoreData };
+createStore(
+ {
+ yourDetail, // it's an object of your state { firstName: '', lastName: '' }
+ },
+ {
+ middleWares: [log], // an array of middleWares, which gets run each actions
+ syncStores: {
+ // you can sync with external store and transform the data
+ externalStoreName: 'externalStoreName',
+ transform: ({ externalStoreData, currentStoreData }) => {
+ return { ...externalStoreData, ...currentStoreData };
+ },
},
- }
- // alternative you can just specify the store name and root state name { yourDetails: { firstName: '' } }
- // syncStores : {
- // externalStoreName: ['yourDetail'],
- // }
- // or you can pass in an array of transform function
- // syncStores : [
- // {
- // externalStoreName: 'externalStoreName',
- // transform: ({ externalStoreData, currentStoreData }) => {
- // return { ...externalStoreData, ...currentStoreData };
- // },
- // }
- // ]
-})
+ // alternative you can just specify the store name and root state name { yourDetails: { firstName: '' } }
+ // syncStores : {
+ // externalStoreName: ['yourDetail'],
+ // }
+ // or you can pass in an array of transform function
+ // syncStores : [
+ // {
+ // externalStoreName: 'externalStoreName',
+ // transform: ({ externalStoreData, currentStoreData }) => {
+ // return { ...externalStoreData, ...currentStoreData };
+ // },
+ // }
+ // ]
+ },
+);
```
##### π `useStateMachine`
-This hook function will return action/actions and state of the app.
+
+This hook function will return action/actions and state of the app.
```typescript
import { updateUserNameAction, removeNameAction } from './actions/yourDetails';
@@ -88,30 +96,37 @@ import { updateUserNameAction, removeNameAction } from './actions/yourDetails';
const { action, state } = useStateMachine(updateUserNameAction);
const { actions, state } = useStateMachine({
removeNameAction,
- updateUserNameAction
+ updateUserNameAction,
});
// The following examples are for optional argument
-const { actions, state } = useStateMachine({
- removeNameAction,
- updateUserNameAction
-}, {
- removeNameAction: 'removeName',
- updateUserNameAction: 'updateUserName',
-});
+const { actions, state } = useStateMachine(
+ {
+ removeNameAction,
+ updateUserNameAction,
+ },
+ {
+ removeNameAction: 'removeName',
+ updateUserNameAction: 'updateUserName',
+ },
+);
const { action, state } = useStateMachine(updateUserNameAction, {
- shouldReRenderApp: false // This will prevent App from re-render and only update the store
+ shouldReRenderApp: false, // This will prevent App from re-render and only update the store
});
```
β DevTool
Built-in DevTool component to track your state change and action.
+
```jsx
+import { DevTool } from 'little-state-machine-devtools';
+
- {process.env.NODE_ENV !== 'production' && }
-
+ {process.env.NODE_ENV !== 'production' && }
+;
```
+