Skip to content

Conditional root rendering #213

@kwameopareasiedu

Description

@kwameopareasiedu

I have this component which is supposed to show a loader while it checks the auth state and then, if authenticated, show the child.

export function AuthGuard(child: HTMLElement) {
  const authenticating = van.state(true);
  const authenticated = van.state(false);

  auth.onAuthStateChanged((user) => {
    if (user) {
      authenticated.val = true;
    } else navigate(LOGIN_ROUTE);

    authenticating.val = false;
  });

  if (authenticating.val || !authenticated.val) {
    return div(
      { className: "w-full h-full grid place-items-center" },
      Text({ align: "center" }, "Authenticating. Please wait...")
    );
  } else return child;
}

The issue I run into is the tab freezes and from Chrome's tab manager, it just consumes memory at an exponential rate. I'm guessing using the authenticated.val in the function's body (and not in a derive or the content) causes an infinte binding.
Screenshot from 2023-12-26 02-01-06

How do I resolve this?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions