Skip to content

Instantly share code, notes, and snippets.

@JacobWeisenburger
Created March 30, 2023 17:54
Show Gist options
  • Save JacobWeisenburger/68573d7b6c269d4fdfbd0a3fca1a0ba5 to your computer and use it in GitHub Desktop.
Save JacobWeisenburger/68573d7b6c269d4fdfbd0a3fca1a0ba5 to your computer and use it in GitHub Desktop.
How to use some of Zustand in Deno Fresh
import { useSyncExternalStore } from 'https://esm.sh/preact@10.11.2/compat'
import { createStore, StoreApi } from 'https://raw.githubusercontent.com/pmndrs/zustand/v4.3.6/src/vanilla.ts'
function useStore<State> ( store: StoreApi<State> ) {
return useSyncExternalStore( store.subscribe, store.getState )
}
const store = createStore( () => ( { count: 0, foo: 'bar' } ) )
export default function ZustandStore () {
const state = useStore( store )
return <pre>{JSON.stringify( state )}</pre> /* {"count":0,"foo":"bar"} */
}
@JacobWeisenburger
Copy link
Author

I'm not sure if this will work for everything, but at first glance, this seems to work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment