Home > Blockchain >  Query object by id using react-query v4
Query object by id using react-query v4

Time:11-13

I have react component where I call react query

  const blogQuery = useQuery(['blog'], getBlog(id));

Then I have api.js file where I store api calls

export const getBlog = async (id) => {
  try {
    const res = await axios.get(`${API_URL}/blogs/${id}`);
    return res.data;
  } catch (err) {
    throw new Error('An error occurred while fetching Blogs');
  }
};

In my HTML part of react component I have error checker that triggers error if something happend wrong.

blogQuery.error.message
            ? 'error.message = '   blogQuery.error.message
            : 'error = '   blogQuery.error

As a result I receive the following error on the screen

error = Missing queryFn

Why is that happening ?

CodePudding user response:

you need to add getBlog as function, now its just called

change this :

  const blogQuery = useQuery(['blog'], getBlog(id));

to this :

  const blogQuery = useQuery(['blog'], ()=> getBlog(id));
  • Related