So I have the following code using Apollo Graphql,
The component will re-render everytime that I click the button which will trigger an onClick event. If I add some state changes after the then
call in the mutation function, the component will basically re-render twice, one for mutation re-render and one for the other state change re-render and it will create a visual flash on the page.
How do I stop useMutation
from re-rendering the page?
import { useMutation, gql } from '@apollo/client';;
import React from 'react';
const ADD_TAGS = gql`
mutation($ids: [ID!]!) {
addTags(ids: $ids) {
id
}
}
`;
export default () => {
const [addTags] = useMutation(ADD_TAGS, {
onCompleted: () => {console.log('on complete')}
});
console.log('render');
return <div>
This is a test idPages
<button onClick={() => addTags({
variables: {
ids: ['123']
}
}).then(data => console.log('finished', data))}>Add</Button>
</div>
}