0

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: [],
    });
  };
}
5
  • I am a little confused what the getUserData function is supposed to do, also, const data = GetUserDataUseQuery; should be const data = useUserData() (change to lower case and rename for best practice)` since that's a hook Commented Dec 30, 2021 at 15:16
  • 3
    "Which part is wrong?" well, for starters, that's an illegal hook call, hooks can only be called from function components or other hooks. Second, your getUserData returns a function, and I don't see any components, so what's your question? Commented Dec 30, 2021 at 15:17
  • getUserData function has been used in a component and actually dispatches the values to reducer .and I didn't use useUserData because getUserData is not in a component.I've used this type of code for useMutation and it worked correctly Commented Dec 30, 2021 at 15:24
  • the component is in speared place and it calls the getUserData on loading .getUserData is actually an action to dispatch data to reducer Commented Dec 30, 2021 at 15:37
  • You'll need to refactor your code to properly handle your use case then - whatever you have right now above is not correct. Commented Dec 30, 2021 at 15:39

1 Answer 1

2

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()
  // ...
}
Sign up to request clarification or add additional context in comments.

3 Comments

the getUserData() is an action and has been called inside a component to get the data, I've used useMutation in exact way and it works very good
I don't have context of how you've used useMutation before, so I can't comment on that, but given your original code, you can only use hooks inside function components and/or custom hooks - see Breaking the Rules of Hooks.
I found out what to do based on your suggestion.thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.