use-state-api-hooks
React hooks for managing and creating reusable stateful object patterns.
Demo
Install
npm install --save use-state-api-hooksor yarn add use-state-api-hooks
Idea
You can read about the inspiration behind this library here
State API Hooks
useBooleanStateApi
State API for boolean values (T or F states)
Example
;; ; ;
Name | Type | Default | Description |
---|---|---|---|
state | Boolean | State of the boolean object | |
setState | Function(state: Boolean): void | Sets the boolean state | |
setTrue | Function(): void | Sets state to true | |
setFalse | Function(): void | Sets state to false | |
toggle | Function(): void | Toggles boolean state |
useArrayStateApi
State API for arrays (array states)
;; ; ; ;
Name | Type | Default | Description |
---|---|---|---|
state | Array | State of the array object | |
setState | Function(state: Array): void | Sets the array state | |
clear | Function(): void | Empty's the array ([]) | |
reverse | Function(): void | Reverses the array | |
pop | Function(): void | Pops value off of the end of the array (does nothing on empty array) | |
push | Function(...vals: T[]) | Pushes values onto end of the array | |
shift | Function(): void | Removes value from beginning of array (does nothing on empty array) | |
unshift | Function(...vals: T[]) | Pushes values onto beginning of array | |
insertAt | Function(val: T, index: number): void | Inserts value at a given index (Does nothing out of bounds) | |
upsertAt | Function(val: T, index: number): void | Removes value from beginning of array (Does nothing out of bounds) | |
deleteAt | Function(index: number): void | Removes value from beginning of array (Does nothing out of bounds) |
useUniqueArrayStateApi
State API for unique arrays (sets)
Name | Type | Default | Description |
---|---|---|---|
state | Array | State of the array object with unique vals | |
setState | Function(state: Array): void | Sets the array state with unique vals | |
clear | Function(): void | Empty's the array ([]) | |
reverse | Function(): void | Reverses the array | |
toggle | Function(...vals: T[]): void | For each val, either adds it to the array if it doesn't exist, or removes it if it already exists | |
pop | Function(): void | Pops value off of the end of the array (does nothing on empty array) | |
push | Function(...vals: T[]) | Pushes unique values onto end of the array | |
shift | Function(): void | Removes value from beginning of array (does nothing on empty array) | |
unshift | Function(...vals: T[]) | Pushes unique values onto beginning of array | |
insertAt | Function(val: T, index: number): void | Inserts unique value at a given index (Does nothing out of bounds or for nonunique vals) | |
upsertAt | Function(val: T, index: number): void | Removes value from beginning of array (Does nothing out of bounds or for nonunique vals) | |
deleteAt | Function(index: number): void | Removes value from beginning of array (Does nothing out of bounds) |
useCounterStateApi
State API for counters
;; ;
Name | Type | Default | Description |
---|---|---|---|
count | Number | Value of the counter | |
min | Number | Minimum possible value of the counter | |
max | Number | Maximum possible value of the counter | |
setCount | Function(count: Number): void | Sets the counter count | |
setMin | Function(min: Number): void | Sets the counter min | |
setMax | Function(max: Number): void | Sets the counter max | |
increment | Function(): void | Increment the count by 1 (won't go above max) | |
incrementBy | Function(x: Number): void | Increment the count by 'x' (won't go above max) | |
decrement | Function(): void | Decrement the count by 1 (won't go below min) | |
incrementBy | Function(x: Number): void | Decrement the count by 'x' (won't go below min) |
useAnchorElStateApi
State API for anchor elements (ie a button that opens a menu in its location)
;;;;; ;
Name | Type | Default | Description |
---|---|---|---|
anchorEl | React.MouseEvent or null | Anchored element | |
setAnchorEl | Function(element: React.MouseEvent or null): void | Sets the anchored element | |
clearAnchorEl | Function(): void | Clears the anchored element (sets anchorEl state to null) | |
setState | Function(state: {count: Number, min: Number, max: Number}): void | Sets the counter state |
Creating your own StateAPIs
In addition to providing some common stateful object patterns, useStateApiHooks
can be used to build your own stateful api's. This library follows compositional factory patterns, where each stateful api has a state api factory describing the state api interface. The useStateApi
hook is a general hook at the base of every state api hook that takes a state api factory as a first argument, and an initial state as a second argument.
useStateApi(<yourStateApiFactory>, <initialState>);
From there, it memoizes the state and state methods, and returns your state api hook.
useStateApi example
Below is an example of how you would use useStateApi
to create a boolean stateful object using JS. If you are using TS, here is the source code.
// this is how to create useBooleanStateApi is created using JS; const booleanStateApiFactory = ; const useBooleanStateApi = ;
useStateApi compositional architecture
For scalable architecture, useStateApiHooks
suggests using compositional factory patterns. This will help prevent architectural problems associated with classical inheritance, and will give you decoupled reusable factory methods.
// mammalMethods.jsconst play = {...}const walk = {...}const run = {...} // useCatStateApi.js;; const catStateApiFactory = { return // these are methods imported from mammalMethods. // setState will pass the state into each function // these are specific cat methods {...} {...} ;}; const useCatStateApi = ; // useDogStateApi.js;; const dogStateApiFactory = // these are methods imported from mammalMethods // setState will pass the state into each function // these are specific dog methods {...} {...}; const useDogStateApi = ;
License
MIT © BenBrewerBowman
This hook is created using create-react-hook.