Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

423
Views
React Native: la variable no se actualiza correctamente al recuperar datos de AsyncStorage

Estoy tratando de almacenar y obtener datos que obtengo de una API. Se supone que el usuario debe obtener un token en la pantalla de inicio de sesión, y el token se mostrará en un cuadro de diálogo de Alerta en la pantalla de inicio cuando el usuario presione un botón. Pero el token no se muestra en el cuadro de diálogo Alerta. el token se muestra después de recargar (no actualizar la aplicación. Usé la extensión Live Server) la pantalla tres veces.

Iniciar sesión.js

 const _userLogin = () => { fetch(URLs._login, { method: "POST", headers, body, }) }).then((response) => response.json()) .then((result) => { if(result.message !== "Unauthorized / Access Token Expired" && result.message !== "The given data was invalid."){ storeData(result.access_token, result.token_type); navigation.navigate('HomeScreen'); } else { Alert.alert("Error", result.message); } }); }; const storeData = async (accessToken, tokenType) => { try { await AsyncStorage.setItem('@access_token', accessToken); await AsyncStorage.setItem('@token_type', tokenType); await AsyncStorage.setItem('@user_auth', tokenType + " " + accessToken); } catch (e) { console.log(e); } }

Inicio.js [ACTUALIZAR]

 const [inputs, setInputs] = React.useState({ userToken: '', userPointsBalance: '', expiringOn: '', }); useEffect (() => { _dashboard(); }) const getToken = async () => { inputs.userToken = await AsyncStorage.getItem('@user_auth'); } const _dashboard = () => { getToken(); fetch(URLs._dashboard, { method: "GET", headers: { 'Authorization': inputs.userToken, 'Content-Type': 'application/json', }, }).then((response) => response.json()) .then(async (result) => { storeData(result.code, result.name, result.member_name, result.user_points_balance, result.expiring_on, result.status, result.token_id); getData(); }); }; const storeData = async (code, name, memberName, userPointsBalance, expiringOn, status, tokenId) => { try { await AsyncStorage.setItem('@user_points_balance', userPointsBalance.toString()); await AsyncStorage.setItem('@expiring_on', expiringOn.toString()); } catch (e) { console.log(e); } } const getData = async () => { const userPointsBalance = await AsyncStorage.getItem('@user_points_balance'); const expiringOn = await AsyncStorage.getItem('@expiring_on'); setInputs({userPointsBalance: userPointsBalance, expiringOn: expiringOn}); } return ( <Text>{inputs.expiringOn}<Text> )
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

espero que funcione

 .then(async(result) => { if(result.message !== "Unauthorized / Access Token Expired" && result.message !== "The given data was invalid."){ await storeData(result.access_token, result.token_type) .then(res=> navigation.navigate('HomeScreen') ) } else { Alert.alert("Error", result.message); } });
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!