How to change parameter value on refetch(react-query, useQuery)?

Viewed 12

I want to call refetch with dynamic parameter. Although I tried it like this↓, I failed. It only send initial value of searchInput. How can I call Api with changed data(value) on refetch method whenever I want?
Now, I use temporary way. I use useEffect for calling refetch. But I know it is not good way. I want to know the best way.

Is there something wrong with it?

// function part
let searchInput = useRef(null);    
const {refetch} = useCommentListQuery({
    postId: postId,
    keyword: searchInput.current.value,
    options: {
        enabled: false,
        onSuccess: (data) => {
            console.log(data);
        },
    },
});

const searchComment = () => {
    refetch().then(r => console.log(r.data));
};



// render part
  <input
      onKeyDown={searchComment}
      ref={searchInput}
      type="text"
  />
0 Answers
Related