Next.js Discord

Discord Forum

how to use hook inside useEffect without using hook inside useEffect

Unanswered
Forest yellowjacket posted this in #help-forum
Open in Discord
Forest yellowjacketOP
so im using trpc (not like it matters) and trpc has this coll thing called useQuery
i want this to only run once so my assumption was to use a useEffect hook
but it doesnt seem to work since useQuery is techincally a hook
how to properly fix this issue of mine

4 Replies

Forest yellowjacketOP
  useEffect(() => {
    setAccessToken(localStorage.getItem('accessToken'))
    const accessToken = localStorage.getItem('accessToken')
    if (accessToken) {
      trpc.session.get.useQuery({ accessToken })
    }
  }, [])

this is the problem im trying to fix
one thing ive tried doing is
  const [User, setUser] = useState<{ email: string, username: string, id: string } | null>(null)
  const [accessToken, setAccessToken] = useState<string | null>(null)

  useEffect(() => {
    setAccessToken(localStorage.getItem('accessToken'))
  }, [])

  if (accessToken) {
    setUser(trpc.session.get.useQuery({ accessToken }).data!)
  }

basically putting the hook outside and setting the variable passed on in a useState
but this throws a rendered multiple times error
and also hutrs performance