T
TanStack3y ago
rare-sapphire

Correct way of fetch with query?

Is this the correct way to fetch with a search query?
import { useQuery } from "@tanstack/react-query";
import axios from "axios";

const fetchData = async (query) => {
const response = await axios.get(`http://localhost:3000/?q=${query}`);
return response.data;
};

export const useFetchData = (query) => {
return useQuery(["users"], fetchData(query));
};
import { useQuery } from "@tanstack/react-query";
import axios from "axios";

const fetchData = async (query) => {
const response = await axios.get(`http://localhost:3000/?q=${query}`);
return response.data;
};

export const useFetchData = (query) => {
return useQuery(["users"], fetchData(query));
};
1 Reply
other-emerald
other-emerald3y ago
No
export const useFetchData = (query) => {
return useQuery(["users", query], ()=>fetchData(query));
};
export const useFetchData = (query) => {
return useQuery(["users", query], ()=>fetchData(query));
};
Also, this syntax is no longer valid in v5

Did you find this page helpful?