S
SolidJS16mo ago
lars

Confusion around solid-query

coming from Angular, so I'm new to both solidJS and solid-query/react-query, when writing a useAuth hook, how would you go about fetching and storing the initial session?
export const useAuth = () => {
const [session, setSession] = createSignal<Session | null>(null);

/** Instance of auth client. */
const { auth } = createClient();

const initialSession = () => {
return createQuery({
queryKey: () => ['getSession'],
queryFn: () => auth.getSession().then(res => res.session),
});
};

setSession(initialSession()?.data ?? null); // <- this doesn't work as expected

return { session }
}
export const useAuth = () => {
const [session, setSession] = createSignal<Session | null>(null);

/** Instance of auth client. */
const { auth } = createClient();

const initialSession = () => {
return createQuery({
queryKey: () => ['getSession'],
queryFn: () => auth.getSession().then(res => res.session),
});
};

setSession(initialSession()?.data ?? null); // <- this doesn't work as expected

return { session }
}
1 Reply
shogun2077
shogun207716mo ago
You can go about setting the initial session using the onMount lifecycle function
onMount(() => {
setSession(initSession())
})
onMount(() => {
setSession(initSession())
})