亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

將<Mutation>改成useMutation

將<Mutation>改成useMutation

嚕嚕噠 2022-05-26 10:20:55
我有一個 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.即使突變有效,有什么方法可以在我的情況下修改和使用相同的函數進行錯誤處理?
查看完整描述

1 回答

?
一只斗牛犬

TA貢獻1784條經驗 獲得超2個贊

沒有理由再擁有一個RemoveUserMutation-removeUser已經在范圍內,所以只需使用它。


function submitForm(email: string) {

  setIsSubmitted(true);

  removeUser({

    variables: {

      email,

    },

  })

  ...

}

您可以像這樣繼續使用您的submitForm功能:


onSubmit={e => {

  e.preventDefault();

  submitForm(email);

}}

這條線


removeUser({variables: {todo: email }})

不工作,因為沒有todo變量。由于您使用 TypeScript,因此您應該為查詢生成類型定義,然后將它們與鉤子一起使用。這將防止這樣的錯誤。


查看完整回答
反對 回復 2022-05-26
  • 1 回答
  • 0 關注
  • 144 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號