score:25
when usequery
is called by the component, it triggers the query subsequently.
but when uselazyquery
is called, it does not trigger the query subsequently, and instead return a function that can be used to trigger the query manually.
it is explained on this page:
https://www.apollographql.com/docs/react/data/queries/#manual-execution-with-uselazyquery
when react mounts and renders a component that calls the
usequery
hook, apollo client automatically executes the specified query. but what if you want to execute a query in response to a different event, such as a user clicking a button? theuselazyquery
hook is perfect for executing queries in response to events other than component rendering. this hook acts just likeusequery
, with one key exception: whenuselazyquery
is called, it does not immediately execute its associated query. instead, it returns a function in its result tuple that you can call whenever you're ready to execute the query.
score:1
something that seems not to be spoken about that i just realize is that uselazyquery
doesn't read from the cache. this is somewhat similar to calling the refetch
function returned from usequery
.
score:4
update:
uselazyquery now returns a promise as of apollo client 3.5.0 (2021-11-08)
score:16
suppose you have a component where you call usequery, then as soon as the component mounts, usequery runs and the data is fetched from the server. but if you use uselazyquery in that component instead of usequery, query doesn't run and data isn't fetched when component mounts. instead you can run the query based on your requirement, say after clicking a button. example:
import react, { usestate } from 'react';
import { uselazyquery } from '@apollo/client';
function delayedquery() {
const [dog, setdog] = usestate(null);
const [getdog, { loading, data }] = uselazyquery(get_dog_photo);
if (loading) return <p>loading ...</p>;
if (data && data.dog) {
setdog(data.dog);
}
return (
<div>
{dog && <img src={dog.displayimage} />}
<button onclick={() => getdog({ variables: { breed: 'bulldog' } })}>
click me!
</button>
</div>
);
}
here, as soon as you click the button, then only the query runs and data is fetched and the image is displayed. but if you had used usequery instead, before clicking the button (i.e when the component mounts), the data would have been fetched and the image would have been displayed
Source: stackoverflow.com
Related Query
- What is the difference between useQuery and useLazyQuery in Apollo graphQL?
- What is the difference between React Native and React?
- What is the difference between state and props in React?
- What is the difference between HashRouter and BrowserRouter in React?
- What is the difference between .ts and .tsx extensions. Both are used as extensions for typescript files in react. So where should we use them?
- What is the difference between redux-thunk and redux-promise?
- What is the difference between componentWillMount and componentDidMount in ReactJS?
- What is the difference between NextJs and Create React App
- What is the difference between jest.fn() and jest.spyOn() methods in jest?
- What is the technical difference between .jsx and .js files
- What is the difference between hashHistory and browserHistory in react router?
- What is the difference between Reactjs and Rxjs?
- What is the difference between .env.local and .env.development.local?
- What is the difference between Jest and enzyme?
- What is the difference between JavaScript and JSX?
- What is the difference between `PropTypes.node` and `PropTypes.any` in react?
- What is the difference between render and return in reactjs?
- What is the difference between Redux Thunk and Redux Saga?
- what is the difference between import and const and which is preferred in commonjs
- What is the difference between Box and Grid in Material-UI?
- What is the difference between @material-ui and @mui
- What is the difference between .js, .tsx and .jsx in React?
- React Native - What is the difference between StyleSheet.absoluteFill() and StyleSheet.absoluteFillObject()?
- react-redux: What is the difference between state.setIn() and state.set()?
- In React, what is the difference between onKeyUp and onKeyUpCapture (and onKeyDown/Capture)?
- What is the main difference between using React-Redux Hooks and React-Redux Connect()?
- What is the main difference between React Query and Redux?
- what is the difference between ( )=>React.FC and ( )=>JSX.Element
- what is the difference between React.HTMLProps<> and React.HTMLAttributes<T>?
- What is the difference between "Drop Down Menu" and "Select Field" in Material-UI
More Query from same tag
- Use nextjs routing outside react hooks in regular javascript
- Needing help trying to solve: 'state' is not defined no-undef
- React - persist state in local storage using react-numeric-input
- React / ES6 - Why calling a function inside another only works with es6 arrow functions?
- Trying to install npm node-sass
- How I can customize antd tab active bottom tab bar color
- How this TS type is found (example with React and final-form)?
- Passing props between react components
- How do I set/get values of one component from another?
- How to bypass CORS policy when sending get/post request from React JS?
- React - Overwrite part of the state.object's values whilst preserving the old ones.
- How to fix updating the same info twice in redux?
- MUIDataTables - Search always open
- Is it safe to change a ref's value during render instead of in useEffect?
- What is the benefit of using :local in CSS files?
- Which is the best practise for using axios in React Project
- How to set a react use state array from onSelectAll checkbox in react bootstrap table
- ReactJS onClick arguments not getting passed to parent component
- material-ui How change initial rowsPerPage in TablePagination
- Scroll View inside view not working react native
- How to properly avoid error "Too many re-renders in reactjs"
- How to access light and dark variants of React Material-UI primary and secondary colors?
- Want to create Drag and Drop Component using React Js
- React span space in between text
- React Hooks, state not updating
- Overriding Material-UI Select Style
- How to remove React hook in certain conditions?
- onClick action not triggered using regular function in React
- Click event won't fire on click on iOS devices but works as intended on desktop
- Cookie not setting in Scala Play