-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add global state using zustand (schema wip)
- Loading branch information
1 parent
f465c62
commit 63f30b3
Showing
5 changed files
with
88 additions
and
9 deletions.
There are no files selected for viewing
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { useStore } from '@/lib/store/store'; | ||
import { create } from 'zustand'; | ||
|
||
interface PlayerState { | ||
name: string; | ||
country: string; | ||
age: number; // In months | ||
money: number; | ||
primaryValues: { | ||
health: number; | ||
morale: number; | ||
intellect: number; | ||
looks: number; | ||
}; | ||
} | ||
|
||
export const usePlayer = create<PlayerState>()((set) => ({ | ||
name: 'Player', | ||
country: 'Undefined', | ||
age: 12 * 18, | ||
money: 0, | ||
primaryValues: { | ||
health: 100, | ||
morale: 90, | ||
intellect: 80, | ||
looks: 70, | ||
}, | ||
})); | ||
|
||
export const useGetPrimaryValues = () => { | ||
const _values = useStore(usePlayer, (state) => state.primaryValues); | ||
return _values; | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { useEffect, useState } from 'react'; | ||
|
||
export const useStore = <T, F>( | ||
store: (callback: (state: T) => unknown) => unknown, | ||
callback: (state: T) => F | ||
) => { | ||
const result = store(callback) as F; | ||
const [data, setData] = useState<F>(); | ||
|
||
useEffect(() => { | ||
setData(result); | ||
}, [result]); | ||
|
||
return data; | ||
}; |
63f30b3
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
lifely – ./
lifely-git-master-carboxydev.vercel.app
lifely.carboxy.tech
lifely-carboxydev.vercel.app