title: Zustand Error: Invalid hook call. Hooks can only be called inside of the body of a function component
body: <p>Getting error</p>
  <blockquote>
  <p>Error: Invalid hook call. Hooks can only be called inside of the body
  of a function component.</p>
  </blockquote>
  <p>in a next.js environment when I try to use <em>Zustand</em> inside a utility function <code>/utils/myfunction.js</code> as:</p>
  <pre><code>import { connectToDatabase } from &quot;./mongodb&quot;
  import {userStore} from '@/utils/store'
  
  export async function userSubs(userId, token) {
    const { db } = await connectToDatabase()
  
    const userSubs = userStore(state =&gt; state.userSubs)
    const setGUserSubs = userStore(state =&gt; state.setGUserSubs)
    ...
  </code></pre>
  <p>how can I store/retrieve states with <em>Zustand</em> in a function or api then ? can't find a documentation about it</p>