I am working on a React JS project. I am using React Hooks (functional components) and React query.
I normally run/ execute the query as follow in the functional component.
const ItemList = (props) => {
let getItemsQuery = useQuery([ 'get-items' ], getItems, {
retry: false,
refetchOnWindowFocus: false
});
// The rest of the code goes here
}
Basically the query is executed when the component is loaded so that the API call is made too.
Now, I am looking for a way where I can only execute the query when a button is clicked.
return (
<button onClick={() => {
//make the call. The value or value might be overridden when the API call is made when the Call 2 button is clicked
}}>Call 1</button>
<button onClick={() => {
//make the call. The value or value might be overridden when the API call is made when the Call 1 button is clicked
}}>Call 2</button>
)
As you can see in the dummy code above, the comment explains what I want to achieve. How can I do that?