Why is this re-rendering infinitely

This block of code is causing infinite re rendering causing the app to crash with this error in console: Unique constraint failed on the constraint: PRIMARY ❌ tRPC failed on user.createUser: Invalid prisma.user.create() invocation:
9 Replies
cje
cje14mo ago
You don’t want to run a mutation on component render
goldenretriever
goldenretriever14mo ago
Yeah. Probably useEffect will help. But overall this problem shouldn’t exist that you are signedIn, but not in a DB
cje
cje14mo ago
doing it on useEffect is still bad, even with an empty dependency array you don't want to mutate resources on page load ever dangerous pattern
goldenretriever
goldenretriever14mo ago
It would solve the issue quickly, but I’d just check if user’s in a DB and do the mutations on sign in itself
Sugan_Selvam
Sugan_Selvam14mo ago
@goldenretriever / @cje I understand using useeffect is a bad idea, thats why I didnt add it in the code. Any inputs on how do I use mutation on sign in itself ?
del.hydraz
del.hydraz14mo ago
Correct me if I’m wrong, but could you run the mutation in a getServerSideProps function? Theoretically that way it should only run once
goldenretriever
goldenretriever14mo ago
I’d need to see the code to say anything about that
theo
theo14mo ago
This is a terrible idea Clerk webhook or as a blocking step in like an api.trpc.getUserProfile.useQuery() call
del.hydraz
del.hydraz14mo ago
Good point, I forgot about the useQuery hook, thanks for the correction