AKumar
AKumar

Reputation: 401

Alert in React Native goes away as the state changes

I am calling forgotPassword api in the app and once its response comes from and state changes in reducer i get callback as i have useEffect in my component. I want to show user an alert that his password is reset successfully, but it shows only for 1 sec and goes away, i think whole view renders again. How can i make alert to staty until use taps on the button in alert.

useEffect(() => {
if (forgotPasswordData != null) {
  Alert.alert('Success',forgotPasswordData.message, [{
    text: 'OKAY',
    onPress: () => {
      dispatch({
        type: authActions.RESET_FORGOT_PASSWORD_DATA,
      });
      console.log("Hello")
      setEmail('');
      setselectedAssociation(null);
      setAssociationName('');
    }  
   }]
);   

} }, [forgotPasswordData]);

Upvotes: 0

Views: 614

Answers (1)

Nooruddin Lakhani
Nooruddin Lakhani

Reputation: 6967

When forgotPasswordData reset, useEffect will rerender again so the best option to alert the user is where you're getting a response.

You can create a callback function and define Alert in it and pass this callback to action and in Saga where you get a result, you can trigger this callback so It will Alert on view.

Upvotes: 1

Related Questions