Best approach for using createResource
I have a question. The
createResource
should be used outside the component and not inside the component, is this a correct statement? I only ask because I noticed the fetcher runs twice if put inside a component vs having it outside the component?2 Replies
It's the opposite and if you're seeing this then something's wrong.
Also here https://discord.com/channels/722131463138705510/1266679656329773117
@mdynnl I think I know the issue... thanks for getting back to me!
I have the following
app.tsx
Then I have an AuthGuard
that basically checks whether you have an auth session or not. If you visit /
and have no session it redirects you to the login page auth/login.tsx
I think the problem here is, it automatically runs createResource
on the /
page first then directs you to auth/login.tsx
when you then log in it then directs you back to /
which runs this twice.
This is the AuthGuard
page:
I then made the index page in app.tsx
lazy load the component for the index page that basically fetches the resource, this definitely doesn't run the fetcher twice and fixes the issue, but its not what i wanted. Do you see what the issue could be here by any chance?