Con*_*top 2 mutation reactjs graphql react-hooks
我想在事件处理程序中使用突变。我的事件定义如下:
(JSX attribute) onOk?: ((e: React.MouseEvent<any, MouseEvent>) => void) | undefined
Run Code Online (Sandbox Code Playgroud)
这就是我从客户端调用突变的方式
export const IMPORT_INFO_MUTATION = gql`
mutation ImportPolicy($data: ImportPolicyInput!) {
importPolicy(data:$data)
{
_id
name
}
}
Run Code Online (Sandbox Code Playgroud)
最终我这样称呼它:
onOk={() => {
useImportPolicyMutation({
variables: {
data: {
jsonData: JSON.parse(importPolicyData)
}
},
})
}
Run Code Online (Sandbox Code Playgroud)
`不用担心突变中的变量和参数。他们很好。问题是我收到以下错误:
Unhandled Rejection (Invariant Violation): Invalid hook call. Hooks can only
be called inside of the body of a function component. This could happen for
one of the following reasons:
1. You might have mismatching versions of React and the renderer (such as React DOM)
2. You might be breaking the Rules of Hooks
3. You might have more than one copy of React in the same app
Run Code Online (Sandbox Code Playgroud)
它是一个钩子,因为 useMutation 的定义如下:
return ReactApolloHooks.useMutation<ImportPolicyMutation, ImportPolicyMutationVariables>(
ImportPolicyDocument,
baseOptions
)
Run Code Online (Sandbox Code Playgroud)
那么,如何在事件处理程序中调用突变呢?
那么,如何在事件处理程序中调用突变呢?
client.mutation()或<Mutation/>组件)useMutation返回旨在按需使用的函数 - 该函数必须在事件处理程序中使用,而不是整个useMutationreact-apollo-hooks 项目描述包含与事件处理程序一起使用的usemutation示例。
| 归档时间: |
|
| 查看次数: |
7078 次 |
| 最近记录: |