T
TanStack10mo ago
grumpy-cyan

fetching data in beforeLoad and injecting it in context

Hey, i've recently started migration from react-router. Still wrapping my head around all the concepts, i'm using context as a vehicle for injecting data by fetching in beforeLoad and then returning data to be available for all the child routes in context. I find this very convenient but am not sure is this a good practice?
3 Replies
conscious-sapphire
conscious-sapphire10mo ago
you can certainly do that. just be aware that beforeLoad runs upon EVERY navigation so you should cache in e.g. query
grumpy-cyan
grumpy-cyanOP10mo ago
I see, thanks. My data fetching lib is rtk-query so there's caching for sure. I'm injecting just the critial top level data for the route like user, project etc. Are there any other downsides of using this approach besides that that i should be aware of?
conscious-sapphire
conscious-sapphire10mo ago
if your context gets really big and you have structuralSharing enabled, you might run into perf issues

Did you find this page helpful?