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

261
Vistas
Why is the account address from metamask coming back as undefined after storing it in useState()

I have this piece of code that is connecting to metamask wallet and sets the account address using useState().

const [currentAccount, setCurrentAccount] = useState("")
const connectWallet = async () => {
    try {
      if (!ethereum) return alert("Please install MetaMask.")

      const accounts = await ethereum.request({ method: "eth_requestAccounts" })

      setCurrentAccount(accounts[0])
      console.log(accounts)
      //   TODO:Add conditional statement to check if user has token
      navigate("/portfolio")
    } catch (error) {
      console.log(error)

      throw new Error("No ethereum object")
    }
  }

 console.log("current account", currentAccount)

  const returnCollection = async (currentAccount) => {
    const options = { method: 'GET', headers: { Accept: 'application/json' } };


    fetch(`https://api.opensea.io/api/v1/collections?asset_owner=${currentAccount}&offset=0&limit=300`, options)
      .then(response => response.json())
      .then(response => console.log("collection owned by current address", response))
      .catch(err => console.error(err));


 useEffect(() => {
   
    returnCollection(currentAccount)
  })

The console is logging the account but when I try to pass it in the returnCollection call in useEffect() it comes back as undefined.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

It seems here, you are doing something like this:

useEffect(() => {
   
    returnCollection(currentAccount)
  })

This will run after every rerender or every state change, and also initially when the component is first mounted, when currentAccount is "". Initially, current account is "", so you may not want to get the collection from "". Instead, maybe what you can do is create another state variable for the result of the returnConnection, and set the state variable to the result of the returnConnection, since you typically don't return results from useEffect, unless it's a cleanup function. Also, maybe you can check the state of currentAccount inside of the useEffect to make sure it's not "", before returning the result.

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