关于成功的反应查询不会触发

发布于 2025-02-01 10:58:03 字数 2202 浏览 3 评论 0原文

我目前有一个无法解释的问题。我与React,TypeScript和React查询一起工作。 我工作的代码是双模式,当单击第二个垃圾按钮时,通过React查询启动调用,然后我们执行onSuccess。

如果我将挂钩呼叫移动到第一个模式中,则可以将其起作用。 如果我将Onsuccess从第二种模式移动到钩子中,它也可以工作。

但是我不明白为什么在这种情况下它不起作用... 有人有想法/解释吗?

提前致谢。

这是第一个模式下面的代码

import React from 'react'
import Button from '../Button'
import SecondModal from '../SecondModal'

interface FirstModalProps {
    isOpen: boolean
    setIsOpen: React.Dispatch<React.SetStateAction<boolean>>
    id?: string
}

const FirstModal = ({ id, isOpen, setIsOpen }: 
FirstModalProps) => {
    const [openRefuse, setOpenRefuse] = React.useState<boolean>(false)

return (
    <>
        <SecondModal isOpen={openRefuse} setIsOpen={setOpenRefuse} id={id} />
        <Button
            onClick={() => {
                setIsOpen(false)
                setOpenRefuse(true)
            }}
            text="refuse"
        />
    </>
)}

export default FirstModal

,然后是第二模式的代码

import React from 'react'
import ConfirmModal from '../../../../../shared/styles/modal/confirm'
import useUpdate from '../../hooks/use-update'

interface SecondModalProps {
    isOpen: boolean
    setIsOpen: React.Dispatch<React.SetStateAction<boolean>>
    id?: string
}

const SecondModal = ({ isOpen, setIsOpen, id }: SecondModalProps) => {
const { mutate: update } = useUpdate()

const updateT = () => {
    update(
        {
            id
        },
        {
            onSuccess: () => {
                console.log('OnSuccess trigger')
            }
        }
    )
}

return (
    <ConfirmModal
        close={() => {
            setIsOpen(false)
        }}
        isOpen={isOpen}
        validate={updateT}
    />
)}

export default SecondModal

,然后是钩子

import { useMutation } from 'react-query'

interface hookProps {
    id?: string
}

const useUpdate = () => {
const query = async ({ id }: hookProps) => {
    if (!id) return
    return (await myApi.updateTr(id))()
}

return useMutation(query, {
    onError: () => {
        console.log('error')
    }
})}

export default useUpdate

I'm currently having a problem that I can't explain. I work with React, typescript and react query.
The code on which I work is a double modal and when clicking on the refuse button of the second one launches a call via react query then we execute an onSuccess.

If I move the hook call into the first modal the onSuccess fires.
If I move the OnSuccess from the second modal into the hook it works too.

But I don't understand why in this case it doesn't work...
Does anyone have an idea/explanation please?

Thanks in advance.

Here is the code below of the first modal

import React from 'react'
import Button from '../Button'
import SecondModal from '../SecondModal'

interface FirstModalProps {
    isOpen: boolean
    setIsOpen: React.Dispatch<React.SetStateAction<boolean>>
    id?: string
}

const FirstModal = ({ id, isOpen, setIsOpen }: 
FirstModalProps) => {
    const [openRefuse, setOpenRefuse] = React.useState<boolean>(false)

return (
    <>
        <SecondModal isOpen={openRefuse} setIsOpen={setOpenRefuse} id={id} />
        <Button
            onClick={() => {
                setIsOpen(false)
                setOpenRefuse(true)
            }}
            text="refuse"
        />
    </>
)}

export default FirstModal

Then the code of the second modal

import React from 'react'
import ConfirmModal from '../../../../../shared/styles/modal/confirm'
import useUpdate from '../../hooks/use-update'

interface SecondModalProps {
    isOpen: boolean
    setIsOpen: React.Dispatch<React.SetStateAction<boolean>>
    id?: string
}

const SecondModal = ({ isOpen, setIsOpen, id }: SecondModalProps) => {
const { mutate: update } = useUpdate()

const updateT = () => {
    update(
        {
            id
        },
        {
            onSuccess: () => {
                console.log('OnSuccess trigger')
            }
        }
    )
}

return (
    <ConfirmModal
        close={() => {
            setIsOpen(false)
        }}
        isOpen={isOpen}
        validate={updateT}
    />
)}

export default SecondModal

Then the hook

import { useMutation } from 'react-query'

interface hookProps {
    id?: string
}

const useUpdate = () => {
const query = async ({ id }: hookProps) => {
    if (!id) return
    return (await myApi.updateTr(id))()
}

return useMutation(query, {
    onError: () => {
        console.log('error')
    }
})}

export default useUpdate

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

彼岸花似海 2025-02-08 10:58:03

回电传递到.mutate函数仅在请求完成时仍在座位安装时执行。相反,始终执行usemuont的回调。这是在此处记录,我也写过关于它在我的博客中>。

callbacks passed to the .mutate function only execute if the component is still mounted when the request completes. On the contrary, callbacks on useMutation are always executed. This is documented here, and I've also written about it in my blog here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文