Quantcast
Channel: Active questions tagged react-native+typescript - Stack Overflow
Viewing all articles
Browse latest Browse all 6287

How to called async function in useEffect after submiting in react native?

$
0
0

I would like once the user click on the submit button for the whole "page" to reload and the useEffect function to be called. The current behavior is that when the user clicks the submit button, the useEffect function is not called at all. It is as if it does not reload directly after submission. I don't know if this is due to async and await. I give you the code :

useEffect() :

useEffect(() => {    console.log('test useEffect');    (async () => {      try {        const value = await AsyncStorage.getItem('authToken_Lust');        if(value !== null) {          const decodedValue = jwt_decode(value);          const current_time = Date.now() / 1000;          if(decodedValue.exp < current_time) {            setMessage('Vous n\'êtes plus connecté(e).')          } else {            setMessage('Vous êtes connecté(e)');          }        }      } catch(err) {        console.log(err);      }    })();  }, []);

The code of the function called after the user clicks submit :

const onSubmit = async () => {    setLoading(true)    await fetch('http://192.168.1.36:3000/api/users/login', {      method: 'POST',      headers: {        Accept: 'application/json','Content-Type': 'application/json'      },      body: JSON.stringify({        email: email,        password: password      })    })    .then((response) => response.json())    .then(async (res) => {      if(res.error) {        setMessage(res.error);      } else {        try {          await AsyncStorage.setItem('authToken_Lust', res.token);        } catch (err) {          console.log(err);        }      }    })    .catch((err) => console.log(err))    .finally(() => setLoading(false));  }

Tanks


Viewing all articles
Browse latest Browse all 6287

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>