Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
N
next.js
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Alex
next.js
Commits
781fa774
Unverified
Commit
781fa774
authored
Jan 06, 2024
by
Niklas Ziermann
Committed by
GitHub
Jan 06, 2024
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
examples: improve zustand example (#58696)
Co-authored-by:
Lee Robinson
<
me@leerob.io
>
parent
779a3734
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
81 additions
and
72 deletions
+81
-72
README.md
examples/with-zustand/README.md
+1
-1
layout.tsx
examples/with-zustand/src/app/layout.tsx
+4
-2
clock.css
examples/with-zustand/src/components/clock.css
+12
-0
clock.tsx
examples/with-zustand/src/components/clock.tsx
+17
-26
counter.tsx
examples/with-zustand/src/components/counter.tsx
+11
-8
StoreProvider.tsx
examples/with-zustand/src/lib/StoreProvider.tsx
+8
-4
store.ts
examples/with-zustand/src/lib/store.ts
+24
-27
useInterval.ts
examples/with-zustand/src/lib/useInterval.ts
+4
-3
tsconfig.json
examples/with-zustand/tsconfig.json
+0
-1
No files found.
examples/with-zustand/README.md
View file @
781fa774
...
@@ -2,7 +2,7 @@
...
@@ -2,7 +2,7 @@
This example shows how to integrate Zustand in Next.js.
This example shows how to integrate Zustand in Next.js.
This is an example on how you can use Zustand
that also works
with Next.js.
This is an example on how you can use Zustand with Next.js.
All client components have access to the Zustand store using
`useStore()`
returned
`store.ts`
file.
All client components have access to the Zustand store using
`useStore()`
returned
`store.ts`
file.
...
...
examples/with-zustand/src/app/layout.tsx
View file @
781fa774
import
StoreProvider
from
'@/lib/
S
toreProvider'
import
StoreProvider
from
'@/lib/
s
toreProvider'
export
default
function
RootLayout
({
export
default
function
RootLayout
({
children
,
children
,
...
@@ -8,7 +8,9 @@ export default function RootLayout({
...
@@ -8,7 +8,9 @@ export default function RootLayout({
return
(
return
(
<
html
>
<
html
>
<
body
>
<
body
>
<
StoreProvider
>
{
children
}
</
StoreProvider
>
<
StoreProvider
lastUpdate=
{
new
Date
().
getTime
()
}
>
{
children
}
</
StoreProvider
>
</
body
>
</
body
>
</
html
>
</
html
>
)
)
...
...
examples/with-zustand/src/components/clock.css
0 → 100644
View file @
781fa774
.clock
{
padding
:
15px
;
display
:
inline-block
;
color
:
#82fa58
;
font
:
50px
menlo
,
monaco
,
monospace
;
background-color
:
#000
;
border-radius
:
4px
;
}
.light
{
background-color
:
#333
;
}
examples/with-zustand/src/components/clock.tsx
View file @
781fa774
...
@@ -2,44 +2,35 @@
...
@@ -2,44 +2,35 @@
import
useInterval
from
'../lib/useInterval'
import
useInterval
from
'../lib/useInterval'
import
{
useStore
}
from
'../lib/store'
import
{
useStore
}
from
'../lib/store'
import
{
useShallow
}
from
'zustand/react/shallow'
const
useClock
=
()
=>
{
import
'./clock.css'
return
useStore
((
store
)
=>
({
lastUpdate
:
store
.
lastUpdate
,
function
useClock
()
{
light
:
store
.
light
,
return
useStore
(
}))
useShallow
((
store
)
=>
({
lastUpdate
:
store
.
lastUpdate
,
light
:
store
.
light
,
}))
)
}
}
const
formatTime
=
(
time
:
number
)
=>
{
function
formatTime
(
time
:
number
)
{
//
cut off except
hh:mm:ss
// hh:mm:ss
return
new
Date
(
time
).
toJSON
().
slice
(
11
,
19
)
return
new
Date
(
time
).
toJSON
().
slice
(
11
,
19
)
}
}
const
Clock
=
()
=>
{
function
Clock
()
{
const
{
lastUpdate
,
light
}
=
useClock
()
const
{
lastUpdate
,
light
}
=
useClock
()
// alternative way to fetch single piece of state:
const
tick
=
useStore
((
store
)
=>
store
.
tick
)
const
tick
=
useStore
((
store
)
=>
store
.
tick
)
// Tick the time every second
useInterval
(()
=>
{
useInterval
(()
=>
{
tick
(
Date
.
now
()
,
true
)
tick
(
Date
.
now
())
},
1000
)
},
1000
)
return
(
return
(
<
div
className=
{
light
?
'light'
:
''
}
>
<
div
className=
{
`clock ${light ? 'light' : ''}`
}
>
{
formatTime
(
lastUpdate
)
}
{
formatTime
(
lastUpdate
)
}
<
style
jsx
>
{
`
div {
padding: 15px;
display: inline-block;
color: #82fa58;
font: 50px menlo, monaco, monospace;
background-color: #000;
}
.light {
background-color: #999;
}
`
}
</
style
>
</
div
>
</
div
>
)
)
}
}
...
...
examples/with-zustand/src/components/counter.tsx
View file @
781fa774
'use client'
'use client'
import
{
useStore
}
from
'../lib/store'
import
{
useStore
}
from
'../lib/store'
import
{
useShallow
}
from
'zustand/react/shallow'
const
useCounter
=
()
=>
{
function
useCounter
()
{
return
useStore
((
store
)
=>
({
return
useStore
(
count
:
store
.
count
,
useShallow
((
store
)
=>
({
increment
:
store
.
increment
,
count
:
store
.
count
,
decrement
:
store
.
decrement
,
increment
:
store
.
increment
,
reset
:
store
.
reset
,
decrement
:
store
.
decrement
,
}))
reset
:
store
.
reset
,
}))
)
}
}
const
Counter
=
()
=>
{
function
Counter
()
{
const
{
count
,
increment
,
decrement
,
reset
}
=
useCounter
()
const
{
count
,
increment
,
decrement
,
reset
}
=
useCounter
()
return
(
return
(
<
div
>
<
div
>
...
...
examples/with-zustand/src/lib/StoreProvider.tsx
View file @
781fa774
'use client'
'use client'
import
{
type
PropsWithChildren
,
useRef
}
from
'react'
import
{
type
PropsWithChildren
,
useRef
}
from
'react'
import
type
{
StoreType
}
from
'./store'
import
type
{
Store
Interface
,
Store
Type
}
from
'./store'
import
{
initializeStore
,
Provider
}
from
'./store'
import
{
initializeStore
,
Provider
}
from
'./store'
const
StoreProvider
=
({
children
,
...
props
}:
PropsWithChildren
)
=>
{
export
interface
PreloadedStoreInterface
extends
Pick
<
StoreInterface
,
'lastUpdate'
>
{}
export
default
function
StoreProvider
({
children
,
...
props
}:
PropsWithChildren
<
PreloadedStoreInterface
>
)
{
const
storeRef
=
useRef
<
StoreType
>
()
const
storeRef
=
useRef
<
StoreType
>
()
if
(
!
storeRef
.
current
)
{
if
(
!
storeRef
.
current
)
{
...
@@ -13,5 +19,3 @@ const StoreProvider = ({ children, ...props }: PropsWithChildren) => {
...
@@ -13,5 +19,3 @@ const StoreProvider = ({ children, ...props }: PropsWithChildren) => {
return
<
Provider
value=
{
storeRef
.
current
}
>
{
children
}
</
Provider
>
return
<
Provider
value=
{
storeRef
.
current
}
>
{
children
}
</
Provider
>
}
}
export
default
StoreProvider
examples/with-zustand/src/lib/store.ts
View file @
781fa774
import
{
createContext
,
useContext
}
from
'react'
import
{
createContext
,
useContext
}
from
'react'
import
{
createStore
,
useStore
as
useZustandStore
}
from
'zustand'
import
{
createStore
,
useStore
as
useZustandStore
}
from
'zustand'
import
{
PreloadedStoreInterface
}
from
'./storeProvider'
interface
StoreInterface
{
export
interface
StoreInterface
{
lastUpdate
:
number
lastUpdate
:
number
light
:
boolean
light
:
boolean
count
:
number
count
:
number
tick
:
(
lastUpdate
:
number
,
light
:
boolean
)
=>
void
tick
:
(
lastUpdate
:
number
)
=>
void
increment
:
()
=>
void
increment
:
()
=>
void
decrement
:
()
=>
void
decrement
:
()
=>
void
reset
:
()
=>
void
reset
:
()
=>
void
}
}
const
getDefaultInitialState
=
()
=>
({
function
getDefaultInitialState
()
{
lastUpdate
:
Date
.
now
(),
return
{
light
:
false
,
lastUpdate
:
new
Date
(
1970
,
1
,
1
).
getTime
(),
count
:
0
,
light
:
false
,
})
count
:
0
,
}
as
const
}
export
type
StoreType
=
ReturnType
<
typeof
initializeStore
>
export
type
StoreType
=
ReturnType
<
typeof
initializeStore
>
const
zustand
Context
=
createContext
<
StoreType
|
null
>
(
null
)
const
store
Context
=
createContext
<
StoreType
|
null
>
(
null
)
export
const
Provider
=
zustand
Context
.
Provider
export
const
Provider
=
store
Context
.
Provider
export
const
useStore
=
<
T
>
(
selector
:
(
state
:
StoreInterface
)
=>
T
)
=>
{
export
function
useStore
<
T
>
(
selector
:
(
state
:
StoreInterface
)
=>
T
)
{
const
store
=
useContext
(
zustand
Context
)
const
store
=
useContext
(
store
Context
)
if
(
!
store
)
throw
new
Error
(
'Store is missing the provider'
)
if
(
!
store
)
throw
new
Error
(
'Store is missing the provider'
)
return
useZustandStore
(
store
,
selector
)
return
useZustandStore
(
store
,
selector
)
}
}
export
const
initializeStore
=
(
export
function
initializeStore
(
preloadedState
:
PreloadedStoreInterface
)
{
preloadedState
:
Partial
<
StoreInterface
>
=
{}
)
=>
{
return
createStore
<
StoreInterface
>
((
set
,
get
)
=>
({
return
createStore
<
StoreInterface
>
((
set
,
get
)
=>
({
...
getDefaultInitialState
(),
...
getDefaultInitialState
(),
...
preloadedState
,
...
preloadedState
,
tick
:
(
lastUpdate
,
light
)
=>
{
tick
:
(
lastUpdate
)
=>
set
({
set
({
lastUpdate
,
lastUpdate
,
light
:
!!
light
,
light
:
!
get
().
light
,
})
}),
},
increment
:
()
=>
increment
:
()
=>
{
set
({
set
({
count
:
get
().
count
+
1
,
count
:
get
().
count
+
1
,
})
}),
},
decrement
:
()
=>
decrement
:
()
=>
{
set
({
set
({
count
:
get
().
count
-
1
,
count
:
get
().
count
-
1
,
})
}),
},
reset
:
()
=>
reset
:
()
=>
{
set
({
set
({
count
:
getDefaultInitialState
().
count
,
count
:
getDefaultInitialState
().
count
,
})
}),
},
}))
}))
}
}
examples/with-zustand/src/lib/useInterval.ts
View file @
781fa774
...
@@ -2,7 +2,10 @@
...
@@ -2,7 +2,10 @@
import
{
useEffect
,
useRef
}
from
'react'
import
{
useEffect
,
useRef
}
from
'react'
// https://overreacted.io/making-setinterval-declarative-with-react-hooks/
// https://overreacted.io/making-setinterval-declarative-with-react-hooks/
const
useInterval
=
(
callback
:
()
=>
void
,
delay
:
number
|
undefined
)
=>
{
export
default
function
useInterval
(
callback
:
()
=>
void
,
delay
:
number
|
undefined
)
{
const
savedCallback
=
useRef
<
typeof
callback
>
()
const
savedCallback
=
useRef
<
typeof
callback
>
()
useEffect
(()
=>
{
useEffect
(()
=>
{
...
@@ -18,5 +21,3 @@ const useInterval = (callback: () => void, delay: number | undefined) => {
...
@@ -18,5 +21,3 @@ const useInterval = (callback: () => void, delay: number | undefined) => {
}
}
},
[
delay
])
},
[
delay
])
}
}
export
default
useInterval
examples/with-zustand/tsconfig.json
View file @
781fa774
{
{
"compilerOptions"
:
{
"compilerOptions"
:
{
"target"
:
"es5"
,
"lib"
:
[
"dom"
,
"dom.iterable"
,
"esnext"
],
"lib"
:
[
"dom"
,
"dom.iterable"
,
"esnext"
],
"allowJs"
:
true
,
"allowJs"
:
true
,
"skipLibCheck"
:
true
,
"skipLibCheck"
:
true
,
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment