Reactjs 如何获得突变反应?
我在我的reactjs应用程序中成功地完成了这个变异,但是我想得到响应图QL返回的结果,但是在我的reactjs应用程序中,有可能吗 反应代码:Reactjs 如何获得突变反应?,reactjs,graphql,react-hooks,apollo-client,react-apollo-hooks,Reactjs,Graphql,React Hooks,Apollo Client,React Apollo Hooks,我在我的reactjs应用程序中成功地完成了这个变异,但是我想得到响应图QL返回的结果,但是在我的reactjs应用程序中,有可能吗 反应代码: import { useMutation } from "@apollo/react-hooks"; const [addStoreToDB, { error }] = useMutation(addStoreMutation); if (error) { console.log("error:", error);
import { useMutation } from "@apollo/react-hooks";
const [addStoreToDB, { error }] = useMutation(addStoreMutation);
if (error) {
console.log("error:", error);
return <p>Error :(</p>;
}
const handleSubmit = event => {
event.preventDefault();
dispatch(addStoreAction(newStore));
addStoreToDB({
variables: {
name: newStore.name,
address: newStore.address,
description: newStore.description,
phone: newStore.phone,
picture1: newStore.picture1
},
refetchQueries: [{ query: getStoresQuery }]
});
};
UseBartation返回的变异函数是一个承诺。因此,您可以使用
。然后
和。catch
等
addStoreToDB({...stuff})
.then(response => {
//handle response
})
.catch(err =>{
//handle error
})
这当然是可能的。文档中对此进行了非常清楚的介绍:。
usemotation
钩子不会在组件渲染时自动执行传递给它的变异。
addStoreToDB({...stuff})
.then(response => {
//handle response
})
.catch(err =>{
//handle error
})
const [addStoreToDB, { error }] = useMutation(addStoreMutation
{
onCompleted: (data) => {
console.log(data) // the response
},
onError: (error) => {
console.log(error); // the error if that is the case
},
}
);