winsoroaks
winsoroaks8mo ago

suggestions on improving my architecture? (not really convex specific)

hello frontend / convex experts! i notice that im spamming my getImages query functions heavily due to my bad design. 🙈 im building something like an album where users can upload images and save them to an album. i have a next.js page that looks like the following and every time a user clicks on the next button, the 8 images uploaded will rerender again since everything here lives on the same page.tsx. im performing something like useMutation(api.images.save) when a user clicks on the "next" button and then they are redirected to a new album page /album-page/2 with 8 images. ideally, the 8 images should not rerender when redirected to the next page from /album-page/1 to /album-page/2. any suggestion? 🙏
No description
24 Replies
erquhart
erquhart8mo ago
You could include the images panel in the layout for /album-page/, that way it stays put and the rest updates when the next segment in the url changes.
winsoroaks
winsoroaksOP8mo ago
thanks! i thought about that. but im doing something like a const [imgs, setImgs] = useState(...) but how can i keep track of which image the user has selected to populate the canvas if it's in the layout page? 😮 a file nested with layout nested with page?
erquhart
erquhart8mo ago
I've barely worked with next.js, but generally in React you want to lift up state in this scenario. If you're keeping things in useState(), I would reach for a context to keep the state accessible to multiple nested components, but there's probably a more Nextjs way to handle this. You using pages router or app router
winsoroaks
winsoroaksOP8mo ago
app router ok a context it is! right, good idea
erquhart
erquhart8mo ago
Oh, and are the uploaded images just in the browser at this point or do they actually go into your convex db right away?
winsoroaks
winsoroaksOP8mo ago
yup in db
erquhart
erquhart8mo ago
If they're in convex you don't need to make your own context then It depends on your app flow whether you want to upload immediately - generally you do for a long flow, as a refresh would wipe out their selections otherwise, unless you're storing them in the browser in a way that survives refreshes. But as you're already doing that, I'd say stick with it, and you can just query convex in any component that needs data about the selections.
winsoroaks
winsoroaksOP8mo ago
ok lemme try the context first and report back. thanks!!
erquhart
erquhart8mo ago
If you're storing the images right away on upload, the context is superfluous, just keep that in mind. Convex's provider already serves as the context you need for convex data. But if you need client state that isn't derived from the database, then you may need your own context.
winsoroaks
winsoroaksOP8mo ago
i actually let users upload all the images on a different page and hit "save" before they are redirected to this album page im not storing right away. sorry i didnt clarify!
erquhart
erquhart8mo ago
But once they're on the album page they're saved, so Convex still has everything you need in the db.
winsoroaks
winsoroaksOP8mo ago
yup right
erquhart
erquhart8mo ago
One of the big benefits of Convex is not having to really do much global state management with contexts and such
winsoroaks
winsoroaksOP8mo ago
yea thats why i was thinking if it's something wrong with my design
erquhart
erquhart8mo ago
I don't think there is! Convex will dedupe any duplicate calls to useQuery(), query away in whatever components need data. And the more you lean directly on Convex's hooks, the more likely you are to stay reactive by default without having to make sure things are getting updated in your state.
winsoroaks
winsoroaksOP8mo ago
i should try lifting the state up first thank you!!
erquhart
erquhart8mo ago
oh you mean how do you know which image they clicked on to edit 🤦‍♂️ yeah you have to lift up state for that lol
winsoroaks
winsoroaksOP8mo ago
yup, i should just remove the "redirect to page/2" when saved. coz it's reactive, i can just keep track of the page number on the page for the user
erquhart
erquhart8mo ago
another way to lift up state that may be better is to put it in the query string in the url that way if they refresh their selection persists as a bonus
winsoroaks
winsoroaksOP8mo ago
oh yea, query string will be super long with the image ID tho?
erquhart
erquhart8mo ago
just my opinion, but as a user, I'd take state that persists when I refresh over a pristine url any day wish more web apps did that by default
winsoroaks
winsoroaksOP8mo ago
good point
erquhart
erquhart8mo ago
but definitely a design choice for you
winsoroaks
winsoroaksOP8mo ago
thanks for the feedback!

Did you find this page helpful?