【问题标题】:How can I get my api return object with useMutation?如何使用 useMutation 获取我的 api 返回对象?
【发布时间】:2022-11-30 11:50:49
【问题描述】:

我使用 C# api,我的 api 在创建元素后返回一个 uid。 但是当我使用 useMutation 时,我没有在响应对象中得到我的 uid。 我怎么才能得到它 ?

function creationTrunk(note) {
    const { isLoading, isSuccess, isError, data, error, mutate } = ReactQuery.useMutation(['createNote'], async () => await fetch(`api/createNote?note=${note}`, {
        method: 'POST'
    }), {
            onSuccess: (res) => {
            console.log(res)
            return res.json()
        },
            onError: (err) => {
            return res.json()
        }
    })

    return { isLoading, isSuccess, isError, data, error, mutate }
}

这就是我的console.log(res)得到的

【问题讨论】:

    标签: reactjs react-query


    【解决方案1】:
        function useCreationTrunk() {    
        
            return useMutation((note) => fetch(`api/createNote`, {
                    method: 'POST',
                    body: note
                }).then(res => res.json()), {
                        onSuccess: (resData) => {
                        console.log(resData)
                        }
                });
        }
    

    然后你可以在你的组件中使用这个钩子。

    const { isLoading, isSuccess, isError, data, error, mutate } = useCreationTrunk();
    const note = {...}
    useCreationTrunkMutation.mutate(note);
    console.log(data);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-28
      • 2019-06-05
      • 1970-01-01
      • 1970-01-01
      • 2013-10-02
      • 2020-08-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多