0

I am using Shopify Polaris to build an application and have a common post data method for fetching data but when I do this I am getting the following error.please help me to do this

Error: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:

  1. You might have mismatching versions of React and the renderer (such as React DOM)
  2. You might be breaking the Rules of Hooks
  3. You might have more than one copy of React in the same app Seefor tips about how to debug and fix this problem.
import Path from './Path';
import StatusValidation from './Validation';
import { useCookies } from 'react-cookie';
import React, {useCallback, useState, useEffect} from 'react';

export function PostBeforeLogin(pathValue, type, userData) {
    return new Promise((resolve, reject) => {
        fetch(Path(pathValue) + type, {
            method:'POST',
            body:JSON.stringify(userData),
            headers: {'Content-Type':'application/json'},
        })
        .then((response) => StatusValidation(response))
        .then((responseJson) => {
            resolve(responseJson);
        })
        .catch((error) => {
            reject(error);
        });
    });
}

export function PostData(pathValue, type, userData, formData = false) {
    const [cookies, setCookie] = useCookies(['acToken']);
    
    return new Promise((resolve, reject) => {
        fetch(Path(pathValue) + type, {
            method:'POST',
            body: formData ? userData :JSON.stringify(userData),
            headers: {
                'Content-Type': formData ? 'multipart/form-data' : 'application/json',
                'Authorization': 'Bearer ' + cookies.acToken
            },
        })
        .then((response) => StatusValidation(response) )
        .then((responseJson) => {
            resolve(responseJson);
        })
        .catch((error) => {
            reject(error);
        });
    });
}
Vengat
  • 119
  • 1
  • 9
  • well can you confirm all those above 3 things are true and then where are you rendering ``? – Red Baron Jun 21 '20 at 13:40
  • Hey i dont use that as a component since i used that as a common function PostData("pathvalue","apiiURL").then(result =>{ console.log(result); }) like this – Vengat Jun 21 '20 at 13:42
  • that's your problem then. the docs say: `Don’t call Hooks from regular JavaScript functions.` – Red Baron Jun 21 '20 at 13:45
  • then is there any other way to call my token inside the headers without using hooks – Vengat Jun 21 '20 at 13:48
  • try my answer, should work – Red Baron Jun 21 '20 at 13:52
  • I hope it will work I was thought about this way also but since I don't want to pass token value from all the components. I want to have this value in only one place. – Vengat Jun 21 '20 at 14:07
  • you shouldn't need to pass through loads of components if you put `useCookie` in the same one was the function invoking – Red Baron Jun 21 '20 at 14:09

1 Answers1

0

try this:

export function PostData(pathValue, type, userData, formData = false, cookies) {
    return new Promise((resolve, reject) => {
        fetch(Path(pathValue) + type, {
            method:'POST',
            body: formData ? userData :JSON.stringify(userData),
            headers: {
                'Content-Type': formData ? 'multipart/form-data' : 'application/json',
                'Authorization': 'Bearer ' + cookies.acToken
            },
        })
        .then((response) => StatusValidation(response) )
        .then((responseJson) => {
            resolve(responseJson);
        })
        .catch((error) => {
            reject(error);
        });
    });
}

pass cookies in as an argument to this function and remove useCookies. then move this code: const [cookies, setCookie] = useCookies(['acToken']); to where you call PostData() and it should work

Red Baron
  • 7,181
  • 10
  • 39
  • 86