调用函数组件体内外部的反应问题

发布于 2025-01-22 03:31:06 字数 938 浏览 2 评论 0原文

因此,我一直在搜索Internet,以获取一个示例的示例,称为函数组件的主体外部。但是,我找不到样本。

我有一种查询类型的GraphQl,我正在等待我可以传递的形式的值。因此,我需要在Formik的Onsubmit函数中称其为。

我已经用

因此,这是我对查询的反应钩,

const [{ data, fetching, stale, error, extensions, operation }] = useForgotPasswordQuery({ variables: { email: "value from the form" } });

与突变不同,它返回一个函数,我可以在其中用来在功能之外调用。

const [, forgotPassword] = useForgotPasswordMutation();

...
<Formik initialValues={{ email: '' }} onSubmit={async (values, { setErrors }) => {
    const response = await forgotPassword({ email: values.email });
}}>

我想在此用例中知道,我无法使用查询类型吗?否则这是我不知道的一种方式。

我已经在这里阅读 stackoverflow 这是约定:

查询 - 用于查询数据(选择操作)

突变 - 用于创建新的和更新/删除现有数据(插入,更新,删除)

以某种方式创建新的和更新/删除现有数据(插入,更新,删除),此公约对我来说很有意义,我想实现它。因此,我试图使用查询而不是突变来做到这一点。

So, I've been searching the internet for an example of a react-query function called outside the body of the function component; however, I was unable to find a sample.

I have a query type of GraphQL, and I am waiting for a value from the form that I could pass on. So I needed to call it inside the onSubmit function of Formik.

I have generated my hooks with GraphQL Codegen. Since we cannot call the react-hooks outside the function, I am uncertain how I can do this.

So, this is my react-hook for the query

const [{ data, fetching, stale, error, extensions, operation }] = useForgotPasswordQuery({ variables: { email: "value from the form" } });

Unlike with the mutation, it returns a function wherein I could use to call outside the function.

const [, forgotPassword] = useForgotPasswordMutation();

...
<Formik initialValues={{ email: '' }} onSubmit={async (values, { setErrors }) => {
    const response = await forgotPassword({ email: values.email });
}}>

I'd like to know on this use case, I cannot use query type? Or this is a way that I am unaware.

I've read here in the Stackoverflow that the convention:

Query — for querying data (SELECT operations)

Mutation — for creating new and updating/deleting existing data (INSERT, UPDATE, DELETE)

In a way, this convention makes sense to me and I want to implement it. So I am trying to do it using the query rather than the mutation.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文