Probably yes. This should not happen. Can you show...
# javascript
m
Probably yes. This should not happen. Can you show some code, which is called when or before this error occurs?
x
Copy code
const { loading, error, data, fetchSupa } = useSupa();

  const onSubmit = (e) => {
    e.preventDefault();
    fetchSupa(() => signIn(form));
  };
  const errorForm = error ? <small className="text-danger">{error}</small> : "";
  if (data) {
    return <Redirect to="/" />;
  }
  return ( ...
custom hook
Copy code
import { useState } from "react";

const useSupa = () => {
  const [data, setData] = useState(null);
  const [error, setError] = useState(null);
  const [loading, setLoading] = useState(false);

  const fetchSupa = async (supaFun) => {
    setLoading(true);
    try {
      const response = await supaFun();
      const { error } = response;
      if (error) throw error;
      setData(response);
    } catch (error) {
      setError(error.message);
      setData(null);
    } finally {
      setLoading(false);
    }
  };

  return { loading, error, data, fetchSupa };
};

export default useSupa;
sign in
export const signIn = (login) => supabase.auth.signIn(login);
any thoughts ? @User
m
I think it might happen when data is set the redirect will be rendered and afterwards loading will be set. But the component is not there anymore.
to test my theory you could add loading to the condition for the Redirect
if (!loading && data) {....}
x
oh i see yea error doesnt show any more
👍
m
Which library are you using for routing?
x
react router
why u ask 😮
m
You could maybe replace the rendering of the component by a function call. Something like this.
Copy code
js
const onSubmit = (e) => {
    e.preventDefault();
    fetchSupa(() => signIn(form)).then(() => router.push("/"));
  };
But this would only work if you throw the error instead of setting it as a state. So maybe just keep it 🙂
This is how I set up my sign in
Copy code
js
const handleSubmit: SubmitHandler<{ email: string; password: string }> = ({
    email,
    password,
  }) => {
    setState("signing-in");
    let redirect = "/dashboard";
    if (typeof router.query.redirect === "string") {
      redirect = router.query.redirect;
    }
    signIn(email, password)
      .then(() => {
        router.push(redirect);
      })
      .catch(() => {
        notify.error(t("error-invalid-credentials"));
        setState("default");
      });
  };