#manage huge loaders
1 messages · Page 1 of 1 (latest)
I would see what you're doing on those loaders and try to move it to different functions
e.g. a function to fetch data from a specific endpoint or a function to query the DB for get some specific resource
like getUserById
is that file in the routes folder?
loads undefined
but its because im not in the fileroute, there is anyway i can load that component from a different file?
Is your goal to just cleanup your page, so the loader data is handled somewhere more clean?
I just write a loaderFunction.server.tsx file, and that seemed to work fine for cleanup.
on routes?
Yea, this is in routes.
on jsx can i return loaderfunction also?
have you used useLoaderData?
yes
That's how you'd get the data from the loader function, into your page as JSX
yes yes, but i want to know if this code works with jsx
you want to call your serverside loader function, client side?
If the function requires request, context, params you cannot use it client side, you have to run it server side and return data
Are you asking if you can execute typescript files in your JS files?
no
You probably should move that loaders file out of the routes folder as it will be understood as an actual url segment
I also use that approach of defining loaders and actions on normal files and import them inside the routes folder
so create a loaders folder on /app/components
and create all the loaders there, and then if i want to use a code on loader i import it
Yep
and how u use it on your component?
/app/components/page/loader
export const loaderFunction: LoaderFunction = async ({ request, context, params }) => {
const url = new URL(request.url);
const data = {
url,
}
return json({ data });
};
/app/routes/page
export const loader: LoaderFunction = async ({ request, context, params }) => {
return await loaderFunction({ request, context, params });
};
Do you mean how to get the data from the loader?
yes from that loader you are exporting
useLoaderData on the route component
ok let me try this, also in a single function how you make 2 prisma fetchs?
``
let userId = await getUserId(request);
let userT = await getUserTotal(userId)
`` this is one function
hi guys, is there a way to use loaders directly like? ```ts
export const userLoader = async () => {
return json(user,{status:200});
}
import { userLoader } from '../loaders/userLoader'
const user = useLoaderData<typeof userLoader>();```
You need to export it from the route
i try - but can't working for me 😦
import { userLoader } from './api/users'
const user = useLoaderData<typeof userLoader>();
<p>{user.name}</p>
``` `TypeError: Cannot read properties of undefined (reading 'name')`
what could be the problem here? Remix 1.7.3
you're not exporting it
you're importing it
you need to export it again
import { userLoader } from "./api/users"
export let loader = userLoader
I recommend you to don't move loaders to another file
move functions you call in your loaders
so instead of userLoader create a getUserById and create a loader that calls getUserById
ty! I forgot about - loader is server side 😄 I just wanted to understand how it works 🤓
yep, or useFetcher with events, or useEffect with initial or something like this 🤓
yeah I don't recommend to do that, use the route loader as much as possible and keep client-side fetching only for things that you don't need initially, like loading autocomplete options