React: Invalid hook call in SubmitForm, custom hook useSendFormData

I made a hook useSendFormData , when i use it i get Invalid hook call.

Hook takes

  • data from SubmitForm

  • url:string,

  • method: post or put

  • success:
    succes message if it was success

  • id: not required but if item has id
    i is added to api call.

      export const useSendFormData = async ({
               formData,
               url,
               method,
               success,
               id,
             }) => {
               const [data, setData] = useState({
                 error: "",
                 loading: true,
                 success: "",
                 data: [],
               });
               const setPartData = (partialData) => setData({ ...data, ...partialData });
               try {
                 if (method === "post") {
                   const { data } = await axios.post(
                     `${SERVER_API}api/v1/${url}/${id ?? ""}`,
                     formData
                   );
                   setPartData({ data, success, error: null });
                 } else if (method === "put") {
                   const { data } = await axios.post(
                     `${SERVER_API}api/v1/${url}/${id ?? ""}`,
                     formData
                   );
                   setPartData({ data, success, error: null });
                 }
    
                 setPartData({
                   loading: false,
                 });
               } catch (err) {
                 const { data } = err.response;
                 setPartData({
                   error: data.error,
                   success: null,
                   loading: false,
                 });
               }
               return {
                 data,
               };
             };
    

    I call it inside Submit Form ,i dont know this matter but i use for handling forms react-hook-forms

     const sendFormData = useSendFormData
    
    
     const handleForm = async (info) => {
         const { data } = await sendFormData({
           formData: info,
           url: "auth/forgot-password",
           method: "post",
           success: "A password reset message has been sent to your email",
         });
    
         console.log(data);
    
         reset();
       };
    

If i change

const sendFormData = useSendFormData

to

const sendFormData = useSendFormData()

I get Cannot destructure property ‘formData’ of ‘undefined’ as it is undefined..How can i make this work and if you see any way of improving this coustom hook I would be very grateful for your help and time.

127 thoughts on “React: Invalid hook call in SubmitForm, custom hook useSendFormData”

  1. Pingback: aralen drug class
  2. Pingback: zanaflex 16 mg
  3. Pingback: nolvadex mexico
  4. Pingback: bimatoprost 03
  5. Pingback: pill for covid