SolidJSS
SolidJSโ€ข3y agoโ€ข
4 replies
pronoob

How can I stop code from executing before?

I am using @solidjs/router for routing, here's the code:
<Router>
        <Routes>
          <Route path="/" component={Guard}>
            <Route path="/" component={Root} />
          </Route>

          <Route path="/login" component={Login} />
        </Routes>
</Router>

The Guard component checks for authentication (whether key is not in localStorage) in createEffect and navigates based on the result. The Root component is a protected component and only authenticated user can access it (localStorage contains the key item).

The Root component looks something like this:
const Root: Component = () => {
  const ws = new WebSocket("...");
  ws.onmessage = e => {
    console.log(e);
  };

  return <h1>Root</h1>;
};

However, the WebSocket is connected before the Guard component validates and navigates the user.
Was this page helpful?