0

得到错误

错误:无效的挂钩调用。钩子只能在函数组件的主体内部调用。

在 next.js 环境中,当我尝试在实用程序函数中使用Zusand/utils/myfunction.js时:

import { connectToDatabase } from "./mongodb"
import {userStore} from '@/utils/store'

export async function userSubs(userId, token) {
  const { db } = await connectToDatabase()

  const userSubs = userStore(state => state.userSubs)
  const setGUserSubs = userStore(state => state.setGUserSubs)
  ...

那么如何在函数或 api 中使用Zusand存储/检索状态?找不到有关它的文档

4

1 回答 1

0

根据官方文档,这可以通过附加到钩子原型的实用功能来实现

import { connectToDatabase } from "./mongodb"
import {userStore} from '@/utils/store'

export async function userSubs(userId, token) {
  const { db } = await connectToDatabase()

  const userSubs = useStore.subscribe(() => {}, state => state.userSubs)
  ...
  useStore.setState({ userSubs: ... })
  ...

查看上面的链接,了解更多关于监听和销毁这些监听器的示例。

于 2021-04-12T10:32:35.197 回答