Updating Class sections based on a selected course

I am creating a testing website. I am trying to create a form where teachers can create a test for one or more sections of a course that they are teaching. Inside the form, the teacher will see the courses that they are teaching and once that is selected I query for the sections in the selected course. The problem is I am not sure how to query for the sections without it throwing a INVALID HOOK CALL error. I have attached the query code the the test form.
2 Replies
djcmurphy
djcmurphy14mo ago
useQuery always needs to be in the root of a component. You can't nest hooks (which trpc calls are) inside functions. Move the useQuery to the route and then do this ... ...getAlllsections.useQuery({courseId:courseId},{enabled:courseId}) The second parameter of usequery can take an object and one of the fiields in that is enabled. enabled means It wont fire until its value is true. You can wrap it like Boolean(courseId) if you need to. Also you don't need to do {courseId:courseId} if they share the same name you can just do ...getAlllsections.useQuery({courseId},{enabled:courseId}) Basically react reads all hooks and calculates state etc for the site/app. As soon as any hook goes into a function or behind a condition it errors because it cant manage state without knowing all the hooks first. A bit weird but that's how it optimises stuff.
Fantastic65
Fantastic6514mo ago
Oh damn thanks man it worked. also i had another question about some other things in the project you think you can help me out a bit