Search code examples
javascriptreactjstypescriptreact-hooksreact-typescript

Send a POST request and receive a response using custom useFetch hook (React.js)


I have used this website to get the logic for a useFetch hook. All I am trying to do is to send a post request and then map the request into a type. I know this will be a simple answer but can't seem to find anything online! Please view the link above for the useFetch hook! :)

EDIT: The body is empty as this is just an example

Error: View screenshot of error

Component:

const requestOptions = {
  method: 'POST',
  headers: {},
  body: JSON.stringify({}),
};
const { data, error } = useFetch<ExampleType>({url, requestOptions});

Solution

  • UseFetch Documentation says you need to pass in a string as the first argument so call the hook with

    useFetch<ExampleType>(url, requestOptions);