Replies: 2 comments 4 replies
-
I would personally use the enabled flag for something like this. You can probably use useEffect to set it back to false once data is fetched. function Example() {
const [filter, setFilter] = useState('');
const [enabled, setEnabled] = useState(false)
const query = useQuery({
queryKey: ['todos', filter],
queryFn: () => getTodos(filter),
enabled
});
return (
<div>
<input
value={filter}
onChange={(e) => setFilter(e.target.value)}
/>
{query.isLoading ? (
'Loading...'
) : query.isError ? (
query.error.message
) : (
<>
<ul>
{query.data.map((todo) => (
<li key={todo.id}>{todo.title}</li>
))}
</ul>
<button
onClick={() => {
setEnabled(true)
}}
>
Refresh
</button>
</>
)}
</div>
);
} |
Beta Was this translation helpful? Give feedback.
1 reply
-
The idea is to split this up into multiple components, and only set state of the parent component when the user hits the |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I'm new to react-query, please help!
In the following example, I want to update filter without invoking queryFn, how can I achieve it?
The queryFn is invoked every time I type a letter in the input, but I only want to invoke queryFn only when button is clicked.
Beta Was this translation helpful? Give feedback.
All reactions