在apollo客户端使用react native调用另一个查询onComplete。

问题描述 投票:0回答:1

我在嵌套查询中得到无效的钩子调用。enter image description here

const fetchNotifications = useNotificationsQuery({
        variables: {
            skip: SKIP,
            take: TAKE,
        },
        async onCompleted(data){
            let ids:Array<string>=[]
            data?.notifications?.forEach((e)=>{
                ids.push(e?.id+"")
            })
            setIds(ids)
            readNotifications()
        }
    });

    const readNotifications =()=> usereadNotifications({
        variables: { notificationIds: ids},
            async onCompleted(data){
                console.log("res"+data)
            }
    })

和usereadNotifications来自于

export const readNotifications = gql` mutation readNotifications($notificationIds: [String]!) {
       readNotifications(notificationIds: $notificationIds) 
} `; 
export const usereadNotifications = (options?: QueryHookOptions) => ( 
     useMutation(readNotifications, options) 
); 
reactjs react-native react-apollo react-apollo-hooks
1个回答
1
投票

usereadNotificationsuseMutation你不能把它包装成一个函数并尝试有条件地执行它,因为它破坏了钩子的规则。

然而useMutation返回给你一个函数,它允许你调用该函数来触发突变。

所以要用它

const fetchNotifications = useNotificationsQuery({
    variables: {
        skip: SKIP,
        take: TAKE,
    },
    async onCompleted(data){
        let ids:Array<string>=[]
        data?.notifications?.forEach((e)=>{
            ids.push(e?.id+"")
        })
        setIds(ids)
        readNotifications()
    }
});

const [readNotifications] = usereadNotifications({
    variables: { notificationIds: ids},
        async onCompleted(data){
            console.log("res"+data)
        }
})
© www.soinside.com 2019 - 2024. All rights reserved.