Skip to content
Discussion options

You must be logged in to vote

I'm also using react-router and SWR's preload. I was surprised that this doesn't behave as I expected:

// In route definition...
loader: () => preload("/users", fetcher),

// In component...
const {data: users} = useSWR("/users", fetcher);

The component data is undefined at first, I guess, because the useSWR hook didn't grab the preloaded data or something.

FWIW, this approach works:

// In component...
const {data: users} = useSWR("/users", fetcher, {fallbackData: useLoaderData()});

Replies: 2 comments 5 replies

Comment options

You must be logged in to vote
5 replies
@n-pandey-f5
Comment options

@koba04
Comment options

koba04 Dec 20, 2022
Collaborator

@n-pandey-f5
Comment options

@koba04
Comment options

koba04 Dec 21, 2022
Collaborator

@SeanDunford
Comment options

Comment options

You must be logged in to vote
0 replies
Answer selected by promer94
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
4 participants