Skip to content

isValidating stays true after 2nd failed request #280

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
mtakac opened this issue Feb 23, 2020 · 2 comments
Closed

isValidating stays true after 2nd failed request #280

mtakac opened this issue Feb 23, 2020 · 2 comments
Labels
bug Something isn't working

Comments

@mtakac
Copy link

mtakac commented Feb 23, 2020

I am trying to implement login using swr, but if my login request fails 2 times in a row isValidating gets stuck and returns true even after the request alredy finished.

Expected behaviour:

I try to login with incorrect credentials
Login request fails
isValidating is set to false no matter how many times the request already failed

Current behaviour:

I try to login with incorrect credentials
Login request fails
isValidating is set to false
But if I try to login with incorrect credentials again
Login request fails
isValidating is not set to false anymore

This is my login component (simplified):

const LoginForm = React.FC () => {
  const [email, setEmail] = useState<string>();
  const [password, setPassword] = useState<string>();
  const [shouldSubmit, setShouldSubmit] = useState(false);

  const { data, error, isValidating } = useSWR(
    shouldSubmit ? ['/signIn', email, password] : null,
    (url, email, password) => authenticationApi.signIn(email, password) 
  );

  useEffect(() => {
    if (!error) return;

    setShouldSubmit(false);
    alert('Invalid email and / or password.');
  }, [error]);

  return (
    <View style={styles.signInForm}>
      <TextInput
        label="Email"
        value={email}
        onChangeText={value => setEmail(value)}
      />
      <TextInput
        label="Password"
        value={password}
        onChangeText={value => setPassword(value)}
        secureTextEntry
      />

      <Button
        mode="contained"
        onPress={() => setShouldSubmit(true)}
      >
        Sign in
      </Button>
    </View>
  );
}
@shuding shuding added the bug Something isn't working label Feb 24, 2020
@dominictwlee
Copy link
Contributor

dominictwlee commented Feb 28, 2020

@quietshu The issue seems to be related to dispatch being reassigned to () => null during cleanup , which gets called before useEffect runs the 2nd time round.

Is there any reason why it's being reassigned that way, and can that be safely removed? If so happy to make a PR for it. Wouldn't it make more sense to just use a plain function rather than useCallback in this case for dispatch?

@shuding
Copy link
Member

shuding commented Aug 28, 2021

Should be fixed with #1396 🙏

@shuding shuding closed this as completed Aug 28, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants