-
Notifications
You must be signed in to change notification settings - Fork 26.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
updated zustand to 4.3.6, changed out deprecated methods, converted t…
…o typescript
- Loading branch information
1 parent
9a1798b
commit b729278
Showing
23 changed files
with
186 additions
and
165 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"extends": "next/core-web-vitals" | ||
} |
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 was deleted.
Oops, something went wrong.
This file was deleted.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,21 @@ | ||
{ | ||
"private": true, | ||
"scripts": { | ||
"dev": "next", | ||
"dev": "next dev", | ||
"build": "next build", | ||
"start": "next start" | ||
"start": "next start", | ||
"lint": "next lint" | ||
}, | ||
"dependencies": { | ||
"@types/node": "18.14.6", | ||
"@types/react": "18.0.28", | ||
"@types/react-dom": "18.0.11", | ||
"eslint": "8.35.0", | ||
"eslint-config-next": "latest", | ||
"next": "latest", | ||
"react": "^18.2.0", | ||
"react-dom": "^18.2.0", | ||
"zustand": "^3.7.1" | ||
"react": "18.2.0", | ||
"react-dom": "18.2.0", | ||
"typescript": "4.9.5", | ||
"zustand": "^4.3.6" | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Binary file not shown.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 2 additions & 6 deletions
8
examples/with-zustand/components/clock.js → ...les/with-zustand/src/components/clock.tsx
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
19 changes: 7 additions & 12 deletions
19
examples/with-zustand/components/counter.js → ...s/with-zustand/src/components/counter.tsx
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,19 @@ | ||
import Link from 'next/link' | ||
import { CSSProperties } from 'react' | ||
|
||
const LinkStyle: CSSProperties = { marginRight: '25px' } | ||
|
||
const Nav = () => { | ||
return ( | ||
<nav> | ||
<Link href='/ssg' style={LinkStyle}> | ||
SSG | ||
</Link> | ||
<Link href='/' style={LinkStyle}> | ||
SSR | ||
</Link> | ||
</nav> | ||
) | ||
} | ||
|
||
export default Nav |
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,15 @@ | ||
import { type PropsWithChildren, useRef } from 'react' | ||
import type { StoreType } from './store' | ||
import { initializeStore, Provider } from './store' | ||
|
||
const StoreProvider = ({ children, ...props }: PropsWithChildren) => { | ||
const storeRef = useRef<StoreType>() | ||
|
||
if (!storeRef.current) { | ||
storeRef.current = initializeStore(props) | ||
} | ||
|
||
return <Provider value={storeRef.current}>{children}</Provider> | ||
} | ||
|
||
export default StoreProvider |
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,60 @@ | ||
import { createContext, useContext } from 'react' | ||
import { createStore, useStore as useZustandStore } from 'zustand' | ||
|
||
interface StoreInterface { | ||
lastUpdate: number | ||
light: boolean | ||
count: number | ||
tick: (lastUpdate: number, light: boolean) => void | ||
increment: () => void | ||
decrement: () => void | ||
reset: () => void | ||
} | ||
|
||
const getDefaultInitialState = () => ({ | ||
lastUpdate: Date.now(), | ||
light: false, | ||
count: 0 | ||
}) | ||
|
||
export type StoreType = ReturnType<typeof initializeStore> | ||
|
||
const zustandContext = createContext<StoreType | null>(null) | ||
|
||
export const Provider = zustandContext.Provider | ||
|
||
export const useStore = <T>(selector: (state: StoreInterface) => T) => { | ||
const store = useContext(zustandContext) | ||
|
||
if (!store) throw new Error('Store is missing the provider') | ||
|
||
return useZustandStore(store, selector) | ||
} | ||
|
||
export const initializeStore = (preloadedState: Partial<StoreInterface> = {}) => { | ||
return createStore<StoreInterface>((set, get) => ({ | ||
...getDefaultInitialState(), | ||
...preloadedState, | ||
tick: (lastUpdate, light) => { | ||
set({ | ||
lastUpdate, | ||
light: !!light | ||
}) | ||
}, | ||
increment: () => { | ||
set({ | ||
count: get().count + 1 | ||
}) | ||
}, | ||
decrement: () => { | ||
set({ | ||
count: get().count - 1 | ||
}) | ||
}, | ||
reset: () => { | ||
set({ | ||
count: getDefaultInitialState().count | ||
}) | ||
} | ||
})) | ||
} |
8 changes: 5 additions & 3 deletions
8
examples/with-zustand/lib/useInterval.js → examples/with-zustand/src/lib/useInterval.ts
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,10 @@ | ||
import StoreProvider from '@/lib/StoreProvider' | ||
import type { AppProps } from 'next/app' | ||
|
||
export default function App({ Component, pageProps }: AppProps) { | ||
return ( | ||
<StoreProvider {...pageProps.initialZustandState}> | ||
<Component {...pageProps} /> | ||
</StoreProvider> | ||
) | ||
} |
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,13 @@ | ||
import { Html, Head, Main, NextScript } from 'next/document' | ||
|
||
export default function Document() { | ||
return ( | ||
<Html lang="en"> | ||
<Head /> | ||
<body> | ||
<Main /> | ||
<NextScript /> | ||
</body> | ||
</Html> | ||
) | ||
} |
Oops, something went wrong.