Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ReferenceError: Cannot access 'useAccount' before initialization #50

Open
Danny-johziy opened this issue Sep 30, 2023 · 6 comments
Open

Comments

@Danny-johziy
Copy link

Danny-johziy commented Sep 30, 2023

Server Error
ReferenceError: Cannot access 'useAccount' before initialization

Please how do I solve this error message?

@Danny-johziy Danny-johziy changed the title Server Error ReferenceError: Cannot access 'useAccount' before initialization Sep 30, 2023
@AndreaZero
Copy link

AndreaZero commented Sep 30, 2023

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?

@Danny-johziy
Copy link
Author

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
Copy link
Author

This path is located on the Components/header.js

@AndreaZero
Copy link

AndreaZero commented Sep 30, 2023

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.

@Danny-johziy
Copy link
Author

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

@AndreaZero
Copy link

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants