Skip to content

Instantly share code, notes, and snippets.

@117
Created June 6, 2024 06:06
Show Gist options
  • Save 117/699e195e67096e1047e78fbe96421b4b to your computer and use it in GitHub Desktop.
Save 117/699e195e67096e1047e78fbe96421b4b to your computer and use it in GitHub Desktop.
zustand-like preact store
import { signal, useSignal } from "@preact/signals";
// zustand.. in only 15 lines :D
export const createStore = <T extends object>(initialState: T) => {
const store = signal(initialState);
return () => {
useSignal(store);
return {
state: store.value,
set: (next: Partial<T>) => (store.value = { ...store.value, ...next }),
reset: () => (store.value = initialState),
};
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment