https://github.com/binfoe/lrhs
lightweight react hook store
https://github.com/binfoe/lrhs
Last synced: 10 months ago
JSON representation
lightweight react hook store
- Host: GitHub
- URL: https://github.com/binfoe/lrhs
- Owner: binfoe
- License: agpl-3.0
- Created: 2023-08-19T06:28:12.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2024-06-06T03:10:58.000Z (about 2 years ago)
- Last Synced: 2025-10-01T09:29:16.873Z (10 months ago)
- Language: TypeScript
- Size: 264 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# light react hook store
> react light weight data share store, with strict type.
## Usage
```ts
// /src/store/somestore.ts
import store from 'lrhs';
const { useStore } = store({
a: 10,
b: 'hello',
});
export { useStore };
// /src/app/pages/somecomponent.tsx
import { useStore } from '@/store/somestore.ts';
const [a, setA] = useStore('a'); //
setA('ok'); // bad. compile error, type of a must be number
setA(10); // ok
setA((oldValue) => oldValue + 1); // can be set function
const [c] = useStore('c'); // bad. compile error, somestore has no propery named c
// another example
import { createStore } from 'lrhs';
const globalStore = createStore({
a: 10,
b: 'hello',
});
globalStore.get('a'); // get property value
globalStore.set('a', 20);
globalStore.set('b', (oldValue) => `${oldValue}.new`);
globalStore.getStore(); // get whole store data object
const [a, setA] = globalStore.useStore('a');
console.log(a);
setA(30);
// hook on updated
globalStore.hook('a', (newValue) => {
localStorage.setItem('SOME_KEY', JSON.stringify(newValue));
});
```