Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

149
Vistas
Accessing variable outside a function in React JS Frontend

I have written this code to access my variable outside the function.The value of res outside the function is undefined. the outside console.log should print the url which is assigned to res inside the function. I will use this response in my anchor tag to open the url in res variable. What am I doing wrong?

const firebaseApp = initializeApp(firebaseconfig)
const storage = getStorage(firebaseApp)
var res;
  const downloadurl = function() { getDownloadURL(ref(storage, 'files/linpeas.txt'))
    .then((url) => {
       res = url
       console.log(res)
    })};
      
console.log(res)
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Try using the useState() to store res and the useEffect() to update res when this changes;

const firebaseApp = initializeApp(firebaseconfig)
const storage = getStorage(firebaseApp)
const [res, setRes] = useState();

const downloadurl = () => { getDownloadURL(ref(storage, 'files/linpeas.txt'))
    .then((url) => {
       setRes(url);
       console.log(res)
    })
    .catch((error) => console.log(error));
};
      
useEffect(() => {
    console.log(res);
  }, [res]);
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda