Way to fire requests using useQuery
with an input onChange
?
#2067
Replies: 4 comments 24 replies
-
I have quite similar challenge, in which case, after a button is clicked I call a function that uses the My function const showModal = async(id: string) => {
trpc.useQuery(["company.find", {id}], {
onSuccess(data) {
// display the modal
displayModal({content: data})
}
});
}
<button onClick={() => showModal('some-id')}>Show</button> |
Beta Was this translation helpful? Give feedback.
-
This should work: import { useRouter } from 'next/router';
function stringOrNull(str: unknown) {
if (typeof str === 'string') {
return str;
}
return null;
}
function Search() {
const router = useRouter();
// We pick the current query string from the router instead of `useState()`
// Allows for reloading the page to see the same search results & to link to it
const q = stringOrNull(router.query.q)?.trim();
const query = trpc.useQuery(['company.find', { q }], {
// Enabled if we have a search query
enabled: Boolean(q),
});
return (
<>
<input
type="search"
name="q"
defaultValue={q}
onChange={(e) => {
// In a real app, you'd probably want to debounce this using something like a `useDebouncedCallback`-hook
router.push(
{
query: {
...router.query,
// Replace query string with new value
q: e.target.value,
},
},
undefined,
{
// Prevent reloading extra stuff and changing the scroll position
shallow: true,
scroll: false,
},
);
}}
/>
<h3>Query results</h3>
<pre>{JSON.stringify(query.data ?? null, null, 4)}</pre>
</>
);
} |
Beta Was this translation helpful? Give feedback.
-
Guyss! I finally think of a great way to do this! I was writing it using next js which has a api exported so the code isn't gonna look like the same as what you guys have, but I hope you guys can get the logic of how I did it, since useQuery is just a wraper around the react-query, and it has the refetch option. Anyone feel free to translate this next js approach to fit the trpc traditional code. export const TestPage = () => {
// State to hold the value
const [value, setValue] = useState();
// Query to fetch company data based on the value
const companyQuery = api.company.getCompany.useQuery({
companyId: value,
}, {
onSuccess: (e) => {
// Update the data here after fetch/re-fetch
console.log(e);
}
});
return < input
type = "search"
onChange = {
(e) => {
// either call refetch here, or update the value
setValue(e);
}
}
/>
}; Leave a like if it helpful! It took me so long to think about this. |
Beta Was this translation helpful? Give feedback.
-
Is there a way to make a direct call to the server that doesn't involve using useQuery? I want to run a query in an on Cick event handler, and then want to do something with the result immediately. I basically ust want to use TRPC to define a standard API endpoint that I can query, await a response, and then use the response within the event handler. That way, I don't need to create a TRPC procedure to execute a query with React Query and then create a separate API endpoint that runs the same query. Could this be done by making a regular My actual use case involves executing a query within the |
Beta Was this translation helpful? Give feedback.
-
One of my endpoints does a search of the db to match with
%LIKE%
. I'm unsure how to write my tRPC query so that it only get's called when the user input changes. I am using Next.js and set it up with the query hooks. I couldn't find any documentation about this. Specifically, I am trying to use this with react-select in order to populate the options list as a user types. Thanks!This was about as far as I got until I realized there's no imperative way to call this query with the default Next.js set up from the guide. I figured I could perhaps set up a vanilla client as well, but was unsure if that was the best way to approach this problem.
Beta Was this translation helpful? Give feedback.
All reactions