0.0.2 • Published 11 months ago
@infinityfx/gs v0.0.2
GS
React persistable global store.
Usage
Creating stores
import { createGlobalStore } from '@infinityfx/gs';
export const useNotesStore = createGlobalStore({
initial: ['My first note'],
// persist the data to local storage (is false by default)
persist: true
});
export const useFruitsStore = createGlobalStore({
initial: {
apple: 0,
banana: 0,
strawberry: 0,
tomato: 0
},
// only persist a subset of data
persist: ['apple', 'banana', 'strawberry'],
// specify a custom key to store data under
key: 'fruits-data'
});Using and mutating data
import { useCounterStore } from './stores';
export default function MyComponent() {
const [data, mutate] = useCounterStore();
// You can update state by mutating the data passed inside the mutate callback
return <button onClick={() => mutate(data => data.counter++)}>
{data.counter}
</button>;
}