Skip to content

Instantly share code, notes, and snippets.

@trezy
Last active August 24, 2022 15:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trezy/5dd6be419201efd613bcd7657df4602b to your computer and use it in GitHub Desktop.
Save trezy/5dd6be419201efd613bcd7657df4602b to your computer and use it in GitHub Desktop.
import { store } from './store.js'
function gameLoop() {
if (!store.getState().isPaused) {
// render game elements
}
requestAnimationFrame(gameLoop)
}
gameLoop()
import { useStore } from './useStore.js'
export function GameUI() {
const {
isPaused,
pause,
play,
} = useStore(state => ({
isPaused: state.isPaused,
pause: state.pause,
play: state.play,
}))
return (
<div className={'menu'}>
<button onClick={pause}>
{'Pause'}
</button>
</div>
)
}
import create from 'zustand/vanilla'
export const store = create((set, get) => ({
isPaused: false,
pause() {
set({ isPaused: true })
},
play() {
set({ isPaused: false })
},
})
import create from 'zustand'
import { store } from './store.js'
export const useStore = create(store)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment