Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

111
Views
useQuery doesn't return the data and the function is returned

I'm trying to get the list of the chats in my app using react-query(useQuery). my API call is in a different component and I'm using Axios to get the data. but the component is returning the function itself not the data .which part is Wrong?

import { useQuery } from "react-query";
import axios from "axios";

const getContacts = async () => {
  const headers = { Authorization: localStorage.getItem("token") };
  const options = {
    method: "GET",
    url: "http://127.0.0.1:8000/chat/mine/",
    headers: headers,
  };
  const { data } = await axios.request(options);

  return data;
};
function GetUserDataUseQuery() {
  return useQuery("getContacts", getContacts);
}
export default GetUserDataUseQuery;

function getUserData() {
  const data = GetUserDataUseQuery;

  return (dispatch) => {
    dispatch({
      type: GET_CONTACTS,
      payload: [],
    });
  };
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I suggest you refactor your code a little to fix some of your problems:

const getContacts = async () => { /*...*/ }
const useGetContacts = () {
  return useQuery('getContacts', getContacts)
}

// `useGetContacts` is a hook, so it should be called inside of a 
// component or another hook - you *cannot* call a hook inside of a 
// simple function, which is what you're attempting to do inside `getUserData`

function MyComponent() {
  const contacts = useGetContacts();
  // ...
}

Alternatively, if you do want to use getContacts as if it was a function, then simply don't wrap it inside a useQuery, since that's what turns it into a hook.

async function getUserData() {
  const data = await getContacts()
  // ...
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!