# Usemutation loops - useMutation on re-render/page loading

**URL:** <https://community.apollographql.com/t/usemutation-loops-usemutation-on-re-render-page-loading/5846>\
**Category:** Client SDKs\
**Tags:** client, web\
**Created:** [March 1, 2023, 11:03pm UTC](https://community.apollographql.com/t/usemutation-loops-usemutation-on-re-render-page-loading/5846 "2023-03-01T23:03:18Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![cyril36](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/cyril36/32/3587_2.png) [@cyril36](https://community.apollographql.com/u/cyril36)\
**Post date:** [March 1, 2023, 11:03pm UTC](https://community.apollographql.com/t/usemutation-loops-usemutation-on-re-render-page-loading/5846/1 "2023-03-01T23:03:18Z")

</div>

Hello,

I would like to register a new user on first connection.  
What i do so far is :

- Login using google/facebook signin
- if the user already exists in my database i get the user information
- if the user does not exist, i create the user

I use to use mutation during a action (like a click) but here i want to check if the user is in the database when they get to the homepage, if he does not exist i want to create it.  
But i dont succeed to do it and the mutation just loop.  
How can i use the useMutation without to be trigger at each rendering?  
here is my code :

> export default function HomeScreen({ navigation }) {  
> [adopter, setAdopter] = useState(false)
> 
> const realmUser = useUser()  
> const {data,loading,error} = getUserByEmail(“cyril.2moreau@gmailc.cs”)  
> const [addUserMutation, { dataAddUser, loadingAddUser, errorAddUser }] = addUser()
> 
> if (error) {alert(error.message)}  
> if (loading) {alert(“loading”)}  
> if (data && data?.getUserByEmail?.error){return Submission error! {data.getUserByEmail.error};}  
> if (data && data?.getUserByEmail?.email){console.log(JSON.stringify(data.getUserByEmail))}  
> /////// IT IS LOOPING ON THE NEXT LINE  
> IF THE USER DOES NOT EXIST I USE useMutation TO CREATE IT  
> if (data && !(data?.getUserByEmail?.userExists)){addUserMutation({variables:{email:“cyril.2moreau@gmailc.cs”, name:realmUser.profile.name}}); console.log(JSON.stringify(data.getUserByEmail))}
> 
> const changeProfile=()=\>{  
> setAdopter(!adopter)  
> }  
> var footer =   
> var body =
> 
> if(adopter){  
> body =   
> footer=  
> }  
> var header =
> 
> return (
> 
> ```
> <View style={styles.container}>
> <StatusBar style="auto" />
> {header}
> <View style={styles.content}>
> {body}
> </View>
> {footer}        
> </View>
> 
> ```
> 
> );  
> }

Thank you for your help

---

<div class="post-metadata">

**Author:** ![Justin\_Gonzalez](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/justin_gonzalez/32/5359_2.png) [@Justin\_Gonzalez](https://community.apollographql.com/u/Justin_Gonzalez)\
**Post date:** [February 6, 2025, 8:49pm UTC](https://community.apollographql.com/t/usemutation-loops-usemutation-on-re-render-page-loading/5846/2 "2025-02-06T20:49:24Z")

</div>


