我用祖传来维持我的人数,但复一页上的数字是连接的,我可以做些什么来加以纠正。
我不敢确定造成这一问题的原因,这似乎是一个服务器和客户水解问题,其原因不正确。
这是我的 app。
use client
import Button from @mui/material/Button
import { useCountStore } from @/app/store/useCountStore
import useStore from ./store/useStore
const Home = () => {
const countStore = useStore(useCountStore, (state) => state)
return (
<>
<div className= text-[red] text-[24px] >{countStore?.num}</div>
<Button onClick={countStore?.increaseNum} variant= contained >increase number</Button>
<Button onClick={countStore?.decreaseNum} variant= contained >decrease number</Button>
</>
)
}
export default Home
这是我的仓库/仓库。
import { create } from zustand
import { persist } from zustand/middleware
interface countType {
num: number,
increaseNum: () => void,
decreaseNum: () => void
}
export const useCountStore = create(persist<countType>((set, get) => ({
num: 0,
increaseNum: () => set({ num: get().num + 1 }),
decreaseNum: () => set({ num: get().num - 1 }),
}), { name: count_storage_tag }))
这是我的仓库/仓库。
use client
import { useState, useEffect } from react
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
}
export default useStore