【发布时间】:2021-11-02 16:08:31
【问题描述】:
我有一个这样的突变(这不是实际的突变调用,而是一个最小的例子):
const App = () => {
const [myMutation] = useMutation(gql`
mutation Test($updateUserId: ID!, $updateUserInput: UpdateUserInput!) {
updateUser(id: $updateUserId, input: $updateUserInput) {
id
firstname
age
}
}
`);
// Runs every time this component is rendered
console.log("rendered");
// Called when the button is clicked
const update = () => {
myMutation({
variables: {
updateUserId: 1,
updateUserInput: {
age: Math.round(Math.random() * 10 + 5) // Set a random age from 5 - 15
}
}
});
};
return (
<>
<h1>Update Test</h1>
<button onClick={update}>Update!</button>
</>
);
};
每当调用onClick 时,都会重新渲染整个组件。这不是我想要的,因为我不关心突变的结果。有什么办法可以阻止myMutation 导致重新渲染,并完全忽略结果?
【问题讨论】:
-
通常,useMutation 不会触发重新渲染,直到您订阅了缓存中的某些值。
-
@RyanLe 你这是什么意思?能举个例子吗?
-
我需要了解您应用的大局。你有你的仓库的链接吗?
-
@RyanLe 抱歉,我的仓库没有链接。是否可以使用 CodeSandbox 或类似的东西来演示它?如果您需要的话,我也可以尝试制作我的程序如何工作的基本框架。
-
当然可以。你可以尝试通过codesanbox重现它。我会直接跳进去。
标签: reactjs apollo apollo-client