nanostores hook & upload btn
This commit is contained in:
parent
7edcd0a49c
commit
d792cec873
5 changed files with 58 additions and 5 deletions
17
src/hooks/nanostores.ts
Normal file
17
src/hooks/nanostores.ts
Normal file
|
@ -0,0 +1,17 @@
|
|||
import { isServer, noSerialize, useSignal, type NoSerialize } from '@builder.io/qwik';
|
||||
import type { ReadableAtom } from 'nanostores';
|
||||
|
||||
export function useNanostore<T>(atom: ReadableAtom<T>) {
|
||||
if (isServer) return
|
||||
|
||||
const state = useSignal<T>(atom.get());
|
||||
const store = useSignal<NoSerialize<ReadableAtom<T>> | undefined>(undefined);
|
||||
|
||||
store.value = noSerialize(atom);
|
||||
const unsubscribe = atom.subscribe((value) => {
|
||||
state.value = value;
|
||||
});
|
||||
|
||||
window.addEventListener('beforeunload', () => unsubscribe());
|
||||
return state;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue