我有一個 removeUser 頁面,我在其中使用 < Mutation >,然后我正在使用該submitForm()函數進行錯誤處理。這段代碼運行良好:export default function RemoveUserPage() { const [isSubmitted, setIsSubmitted] = useState(false); const [isRemoved ,setIsRemoved] = useState(false); const [errorMessage, setErrorMessage] = useState(''); function StatusMessage(){ if (isRemoved){ return ( <CustomAlert severity='success' text='User Removed'></CustomAlert> ) } //else... } function submitForm(RemoveUserMutation: any, email: string) { setIsSubmitted(true); RemoveUserMutation({ variables: { email: email, }, }).then(({ data }: any) => { setIsRemoved(true); }) .catch((error: { message: string; }) => { setIsRemoved(false); console.log("Error msg:" + error.message); setErrorMessage(error.message) }) } return ( <Mutation mutation={RemoveUserMutation} > {(RemoveUserMutation: any) => ( <div> <PermanentDrawerLeft></PermanentDrawerLeft> <Formik initialValues={{ email: '' }} onSubmit={(values, actions) => { setTimeout(() => { alert(JSON.stringify(values, null, 2)); actions.setSubmitting(false); }, 1000); }} validationSchema={schema} > {props => { const { values: { email }, errors, touched, handleChange, isValid, setFieldTouched } = props; const change = (name: string, e: any) => { e.persist(); handleChange(e); setFieldTouched(name, true, false); };但是,我被建議useMutation改用。首先,我無法這樣做,因為我收到了這樣的錯誤:Unhandled Rejection (Error): GraphQL error: Variable `email` of type `String!` must not be null.即使突變有效,有什么方法可以在我的情況下修改和使用相同的函數進行錯誤處理?
將<Mutation>改成useMutation
嚕嚕噠
2022-05-26 10:20:55