S
SolidJS11mo ago
versa

createMemo accessor null value

I have the following code to access a Firebase collection and it depends on a user's UID which gets loaded after the page load:
const docComputed = createMemo(() => collection(db, state.data?.uid ?? "a"));
const todos = useFirestore(docComputed);
const docComputed = createMemo(() => collection(db, state.data?.uid ?? "a"));
const todos = useFirestore(docComputed);
I have a as a placeholder because I can't return null from createMemo. right now what happens is before state.data is populated by solid-firebase, the docComputed value has the dummy value a which doesn't exist in the database. I am wondering if it is possible to make it so that todos is also null or has some indication that it is still loading while the user auth is loading rather than trying to access the nonexistent dummy collection a. here is the relevant source code for solid-firebase for reference: https://github.com/wobsoriano/solid-firebase/tree/main/src/hooks
1 Reply
ai6
ai611mo ago
Is there a reason why you can't use separate signals for that? I'm thinking, since useFirestore can have a nullable initial value, you could create an isLoading signal which can be set to true until state.data?.uid is loaded:
const [isLoading, setIsLoading] = createSignal(true);
const [docComputed, setDocComputed] = createSignal(null);
const [error, setError] = createSignal(null);

createEffect(() => {
if (state.data?.uid) {
setIsLoading(false);
setDocComputed(collection(db, state.data.uid));
setError(null);
} else {
setIsLoading(false);
setError(new Error("User ID not found"));
}
});

const todos = useFirestore(isLoading() || error() ? null : docComputed());
const [isLoading, setIsLoading] = createSignal(true);
const [docComputed, setDocComputed] = createSignal(null);
const [error, setError] = createSignal(null);

createEffect(() => {
if (state.data?.uid) {
setIsLoading(false);
setDocComputed(collection(db, state.data.uid));
setError(null);
} else {
setIsLoading(false);
setError(new Error("User ID not found"));
}
});

const todos = useFirestore(isLoading() || error() ? null : docComputed());