I am trying to understand the correct way to distribute data to components without using props. As far as I can see, there are two ways to achieve this.
The first one is by importing routeLoader:
import { useAuth } from "~/routes/layout";
import { component$ } from "@builder.io/qwik";
export default component$(() => {
const user = useAuth();
return (<p>{user.value.name}</p>);
});
The second one is using useContext:
// inserted value to contextprovider in layout
import { useAuthContextId } from "~/routes/layout";
import { component$, useContext } from "@builder.io/qwik";
export default component$(() => {
const user = useContext(useAuthContextId);
return (<p>{user.value.name}</p>);
});
Also, am I doing something wrong with the usage of useContext? Do we really need to import and pass the context ID? I thought just writing the context ID name would be enough, like this:
const x = createContextId('user.data');
const val = useContext('user.data');
I thought importing routeLoader might be the wrong way to do it, as it would be invoked unnecessarily, but it seems like it is being invoked once regardless of how many times I call it in components or in loops. Then I checked the useContext result, and it contains data from other routeLoader as well (?). So, is routeLoader essentially a wrapper around useContext?
This might be general information for those who know JSX or React already, but this is my first time, and I'm a little bit lost in thought.