Created
June 6, 2024 06:06
-
-
Save 117/699e195e67096e1047e78fbe96421b4b to your computer and use it in GitHub Desktop.
zustand-like preact store
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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