#how can i redirect to another page client side

1 messages · Page 1 of 1 (latest)

spark sky
#

Hi! i want to redirect the user to the dashboard page once they login. how can i do this?

export default function Index() {
  const { supabase } = useOutletContext();

  const handleLogin = async () => {
    const { data, error } = await supabase.auth.signInWithPassword({
      email: "email",
      password: "password",
    });
    if (!error) {
      //redirect dashboard
    }
  };
  return <button onClick={handleLogin}>Login</button>;
}
ripe scroll
#
+ import { useNavigate } from '@remix-run/react';
+
export default function Index() {
  const { supabase } = useOutletContext();
+ const navigate = useNavigate();

  const handleLogin = async () => {
    const { data, error } = await supabase.auth.signInWithPassword({
      email: "email",
      password: "password",
    });
    if (!error) {
      //redirect dashboard
+     navigate('/dashboard');
    }
  };
  return <button onClick={handleLogin}>Login</button>;
}
shell karma
#

Using vanilla JavaScript you can use ```
window.location = another location

jade dragon
# spark sky Hi! i want to redirect the user to the dashboard page once they login. how can ...

useNavigate is the preferred solution for client side redirection. If you're using remix you may want to actually make a post to an action function and then use the redirect method to move the user to the next desired page.

useNavigate will help the remix router give you benefits, like painting only the changes you need. I don't believe the same benefit necessarily happens with vanilla javascript, but it's possible it does.

As for the action recommendation, that's letting remix know something has changed in your application and new data may be required for the user. If you're already redirecting this may not be all that necessary, unless you're redirecting to a different route that makes use of similar loader data