-
I have an atomWithQuery that I'd like to only run after a button click. I have it wired up and working when using an atom to determine enabled state, but when I force it to disabled export const search = atomWithQuery((get) => ({
queryKey: [get(searchTerm), get(searchLocation)],
queryFn: async ({ queryKey: [term, location]}) => {
// Fetch logic
const json = await res.json()
// other stuff
return json
},
enabled: false
})) //A separate hook
const useSearch = () => {
const setEnabledSearch = useUpdateAtom(enabledSearch)
const startSearch = useUpdateAtom(searchResult)
const search = useCallback(() => {
setEnabledSearch(true)
startSearch({ type: 'refetch'})
}, [startSearch])
return { search }
} // Component onClick event <Comp onClick={search} /> |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 12 replies
-
While I think running on button click should be handled in an imperative way (= callback function), there's no such things now except for To avoid suspending with |
Beta Was this translation helpful? Give feedback.
-
Hi there! I made a small sandbox with what I understand your goals to be: https://codesandbox.io/s/vibrant-bhaskara-c5s7f And it seems to be functioning, please let me know if I am missing something. |
Beta Was this translation helpful? Give feedback.
Hi there! I made a small sandbox with what I understand your goals to be: https://codesandbox.io/s/vibrant-bhaskara-c5s7f
And it seems to be functioning, please let me know if I am missing something.