here is the code:
import { type AppType } from 'next/app'
import { api } from '~/utils/api'
import '~/styles/globals.css'
import Nav from '~/components/Nav'
import { useEffect, useState } from 'react'
const MyApp: AppType = ({ Component, pageProps }) => {
const [showCart, setShowCart] = useState(false)
const [loading, setLoading] = useState(false)
const createSession = api.user.createSession.useMutation()
const generateId = async (): Promise<string | undefined> => {
const res = await createSession.mutateAsync()
if (res.response) {
return res.response.id
} else if (res.error) {
return res.error
}
}
const setSessionId = async () => {
const tmp: string | undefined = await generateId()
if (tmp) document.cookie = `sessionId=${tmp}`
setLoading(false)
}
useEffect(() => {
if (!loading) {
setLoading(true)
const cookieString: string = document.cookie
const cookies: string[] = cookieString.split(';') || []
let sessionId: string | null = null
for (let i = 0; i < cookies.length; i++) {
const cookie: string | undefined = cookies[i]
if (!cookie || cookie.trim() === '') {
continue
}
if (cookie.trim().startsWith('sessionId=')) {
sessionId = cookie.trim().substring('sessionId='.length)
break
}
}
if (!sessionId) {
void setSessionId()
}
}
}, [])
return (
<>
<Nav showCart={showCart} setShowCart={setShowCart} />
<h1>{loading && 'LOADING'}</h1>
<Component {...pageProps} />
</>
)
}
export default api.withTRPC(MyApp)
I am using trpc and prisma. for some reason the useEffect is running twice and it is posting to the db twice. any idea what's causing the re-render? the loading is always false for some reason even after I set it to true. is it because I am setting it wrong? I initially thought that maybe it was rendering twice, once in the server and once in the client. but I logged the window object and they were both defined.
any idea what I am doing wrong?