#How to query data without component reloading?
68 messages · Page 1 of 1 (latest)
export default async function DemoPage() {
const authUser = useUser().user
const results = useQuery(api.logger.list, {
user_id: authUser ? authUser.id : "skip"
}) || []
console.log(`loading ${results.length} logs`)
return (
<div className="container mx-auto py-10">
<div className={"flex justify-center mb-10"}>
<h1>Logs</h1>
</div>
<LogTable logs={results as any} />
</div>
)
}
However the problem is, whenever a new log is made, the react component reloads which causes my loading.tsx file for next suspence to load the page. So if logs fire while a user is on the logging page, it refeshes every time which is a bad UE
is there a way I can steam the logs to load into the component without this refesh action?
im thinking maybe i need to switch to pagination?
i know its simple to add
useQuery hooks cause components to rerender, but not remount — so I don't think it's the useQuery causing this here. Could you share the code that includes the suspense boundary?
in the root of my next app dir i have:
loading.tsx
import React, { FC } from "react";
import SpinnerLoader from "@/components/ui/Spinner";
interface ComponentProps {}
const Component: FC<ComponentProps> = ({}) => {
return (
<>
<SpinnerLoader />
</>
);
};
export default Component;
from the first code sample it doesn't look like any suspense would be triggered even if the api.logger.list query where loading (which should only happen on initial load), since it would just be an empty list
well in my LoggerTable i do have some useState that updates the logs from the passed props:
const LoggerTable: FC<LoggerProps> = ({ logs }) => {
const [currentPage, setCurrentPage] = useState(0);
const [logPages, setLogPages] = useState<Logs[]>([]);
useEffect(() => {
// Split logs into pages of 10 logs each
const pages = [];
for (let i = 0; i < logs.length; i += 10) {
pages.push(logs.slice(i, i + 10));
}
setLogPages(pages);
}, [logs]);
}
Could this be it?
How is a new log created, is that what's causing this component to be remounted?
Nothing here would cause an unmount and then a remount
How is a new log created, is it a user action on this page?
no logs are created in different pages in the app and there mutations
Is this the normal useQuery hook imported from convex/react, or are you using a special one that does suspense things?
"convex/react";
Could you share more of your code? I don't see anything that would trigger suspense here. If it's helpful you could DM me the code or add me to a GitHub project, username thomasballinger
The useQuery hook from Convex never triggers a suspense boundary, so I don't see anything in the code above that would cause it .
okay so
https://github.com/neura-labs-ai/web/blob/dev/website/app/(console)/console/logs/page.tsx this is the page file that loads the data for my table.
table - https://github.com/neura-labs-ai/web/blob/dev/website/app/(console)/console/logs/LogTable.tsx
and then the logger convex function - https://github.com/neura-labs-ai/web/blob/dev/website/convex/logger.ts
Oh cool, looks like you just added pagination 📃
yes
thought it would be a good feature in something like a logger which I dont want to have to load 5k logs each page load
would it be helpful if i record with obs my site and how it functions when i use the log table i made?
so you can see what i mean or is the code enough
I think I know what you mean here by the component reloading, you're saying somehow the query updating causes your page to suspend — but sure, that might be helpful to see
To clarify, does this happen if you have the logs view open in one tab and add a long in another browser tab?
yes, this was how i noticed it! I would trigger a log action the go back to the page and was good. But realistically actions will happen without the user going anything do and they will want to see the updates. So if the trigger while on the page you will see the reload every time
ok
There's probably something about Next.js App Router that I'm missing here because I don't see in this code why it would go to a loading state.
okay im going to record it...im just realising i had nvidia drivers from 2023 still, obs wants me to update them 
If you want other code suggestions, I would do the repagination inline in the component (possibly memoized) instead of in a useEffect with setState — but I don't understand how this would cause a loading state, so you could try it but I'd be surprised if it helps
oh thanks, I'm on a plane so harder than usual to install ffmpeg/vlc to watch a mkv
Those double-loads are odd
like when you first click Logs
What does the network tab look like when you do this same navigation?
So you think this could be a reason?
The initial double `logs?_requests are interesting but after that since there are no more network requests this must be Convex data changing, so that's helpful to confirim. I'll take another look
ok
I thought it might be an auth reloading thing but it doesn't look like it
nice!
So all i did was move the query function into the component like you suggested. What i think happed was next as caching the query and then convex invalidated it so it caused the reload in react suspence.
const { results, loadMore } = usePaginatedQuery(api.logger.list, {
user_id: authUser ? authUser.id : "skip"
}, {
initialNumItems: 10,
}) || []
i just moved this into the component itself and not passed in props
Interesting! Since both page.tsx and LogTable have "use client"; I wouldn't have thought this would matter, good to know!
thanks for sharing, glad it's worked out
Thank you for the help! Convex is a great product, really enjoying using it Tom.
also question: Im a college student studying computer science. What kind of experience do I need to ever work somewhere like convex? I have software skills but im not sure whats enough to apply for jobs like these?
@muted oriole
Hm good question, thinking about it.
Re: experience, when we have bandwidth we have previously hired recent graduates, so internships at startups are helpful since that's usually the only practical experience more undergrads have time to get. Experience building web applications is a big bonus, but most of the work at Convex is backend (we're building backend infrastructure so that our customers don't have to!). We write our backend in Rust, but experience with C, Go, C++, Scala, Java, OCaml, or other statically typed compiled language will do here. Operating systems or other data structure-y courses are relevant here.
For
whats enough to apply for jobs like these?
I would go ahead and apply to jobs like these, especially internships, whether you think you're ready or not.
very helpful! thanks for the information tom, good talking to you