youtube-tutorials icon indicating copy to clipboard operation
youtube-tutorials copied to clipboard

ReferenceError: Cannot access 'useAccount' before initialization

Open Danny-johziy opened this issue 1 year ago • 6 comments

Server Error ReferenceError: Cannot access 'useAccount' before initialization

Please how do I solve this error message?

Danny-johziy avatar Sep 30 '23 06:09 Danny-johziy

The error suggests you're accessing useAccount before it's defined.

Ensure:

  • It's declared before use.
  • There's no typo.
  • Imports/exports are correct if it's from another module.

Can you share the code section where useAccount is declared and used?

AndreaZero avatar Sep 30 '23 10:09 AndreaZero

import { useEffect, useState } from "react"; import Image from "next/image"; import Link from "next/link"; import { useAccount, useConnect } from "wagmi"; import { Search } from "@web3uikit/icons"; import styles from "@/styles/Home.module.css";

import Logo from "../public/assets/blurLogo.png";

export default function Header() { const { address, useAccount } = useAccount(); const { connect, connectors } = useConnect();

const [isLoggedIn, setIsLoggedIn] = useState(false);

useEffect(() => {
    if (!isConnected) {
        setIsLoggedIn(true);
      } else {
        setIsLoggedIn(false);
      }
}, [isConnected] );

return (
    <section className={styles.header}>
      <section className={styles.logo}>
        <Link href="/">
          <Image src={Logo} alt="Blur Logo" width="70" height="" />
        </Link>
      </section>
      <section className={styles.nav}>
        <section className={styles.nav_items}>
          <p>COLLECTIONS</p>
          <Link href="/portfolio" className={styles.link}>
            <p>PORTFOLIO</p>
          </Link>
          <p>AIRDROP</p>
        </section>
        <section className={styles.searchSection}>
          <section>
            <span>
              <Search fontSize="25px" />
            </span>
            <input
              placeholder="Search collections and wallets"
              disabled=""
              className={styles.inputField}
            />
          </section>
        </section>
        {isLoggedIn ? (
          <section>
            {connectors.map((connector) => (
              <button
                disabled={!connector.ready}
                key={connector.id}
                onClick={() => connect({ connector })}
                className={styles.connect_btn}
              >
                CONNECT WALLET
              </button>
            ))}
          </section>
        ) : (
          <section className={styles.loggedIn_section}>
            {isLoggedIn ? address?.slice(0, 8) : ""}
          </section>
        )}
      </section>
    </section>
  );

}

Danny-johziy avatar Sep 30 '23 10:09 Danny-johziy

This path is located on the Components/header.js

Danny-johziy avatar Sep 30 '23 10:09 Danny-johziy

You've imported the useAccount function and then, within the Header component, you're attempting to call useAccount() as if it's a function and at the same time, you're trying to extract a variable named useAccount from what that function returns.

This creates confusion and a conflict.

In simpler terms: you're using the same name "useAccount" for both an imported function and an internal variable, and this is causing the error.

You need to decide which one you want and adjust the code accordingly.

AndreaZero avatar Sep 30 '23 11:09 AndreaZero

Please can you give me a little fix of which line of code to make amendment to?

Danny-johziy avatar Sep 30 '23 11:09 Danny-johziy

The error is in this line of code: const { address, useAccount } = useAccount();

To fix it, you should:

Change the variable name you're trying to destructure from the useAccount() function to avoid a naming conflict. If you intended to get the address and another variable (let's call it userDetails for example), the line would look like:

const { address, userDetails } = useAccount();

Then, make sure to update any other parts of your code where you've used the old variable name.

This should resolve the issue!

AndreaZero avatar Sep 30 '23 11:09 AndreaZero