1

I am trying to use reactJS to show the balance of an Ethereum wallet with web3. My code successfuly gets the balance and outputs it to the console, however when i try to assign it as a variable and display it in html i get the following error: Line 19:37: 'etherval' is not defined no-undef

The script:

import React from 'react';
import Web3 from 'web3'

export default function balances() {

const web3 = new Web3(new Web3.providers.HttpProvider('https://****'));
web3.eth.getBalance("0x****", function(err1, balance) {
{
    console.log(web3.utils.fromWei(balance, "ether") + " ETH")
    const etherval = web3.utils.fromWei(balance, "ether");
  }
})

  return (
    <div className="details">
      <div className="container">
        <div className="row">
          <div className="col-md-12">
                      <h2>Balance: {etherval}</h2>

          </div>
        </div>
      </div>
    </div>
  );
}

i have tried to initialise the variable with let but this produces more errors, how can i fix this?

8
  • 1
    Because const are block scoped, so etherval only exists inside the getBalance callback. Commented Jan 10, 2022 at 22:52
  • I think the problem is in: const web3 = new Web3(new Web3.providers.HttpProvider('https://****)); You are missing a '. I should read const web3 = new Web3(new Web3.providers.HttpProvider('https://****')); Commented Jan 10, 2022 at 22:54
  • @GaneshBudhathoki this was my mistake when i removed the url from the example, i have fixed it, thanks Commented Jan 10, 2022 at 23:00
  • @Mike'Pomax'Kamermans thanks, can you post a working example? Commented Jan 10, 2022 at 23:01
  • 1
    don't use var, that's from an era of JS that we no longer live in. Use let for values that are going to change, or get assigned later, and use const for values that you immediately assign and then never change again. But even if you do that as properly as possible, that won't actually work because your return statement will happen well before you get that value. That etherval is state data, and you should be using the useState hook for it. Commented Jan 10, 2022 at 23:47

2 Answers 2

1
import React, {useState} from 'react';
import Web3 from 'web3'

export default function Balances() {

const [balance, setBalance] = useState(0)
const web3 = new Web3(new Web3.providers.HttpProvider('https://****'));
web3.eth.getBalance("0x****", function(err1, balance) {
{
    console.log(web3.utils.fromWei(balance, "ether") + " ETH")
    setBalance(web3.utils.fromWei(balance, "ether"));
  }
})

  return (
    <div className="details">
      <div className="container">
        <div className="row">
          <div className="col-md-12">
                      <h2>Balance: {balance}</h2>

          </div>
        </div>
      </div>
    </div>
  );
}

You can use useState hook and assign the value once you have it. If this version doesn't render the correct amount, you can use useEffect hook also

Sign up to request clarification or add additional context in comments.

8 Comments

for this i get error: Line 7:31: React Hook "useState" is called in function "balances" that is neither a React function component nor a custom React Hook function. React component names must start with an uppercase letter. React Hook names must start with the word "use" react-hooks/rules-of-hooks Line 12:5: 'setBalance' is not defined no-undef
i changed component to capital letter but still get this error: Line 12:5: 'setBalance' is not defined no-undef
I made a typo sorry. when I declared the state on top, it should be like this : const [balance, setBalance] = useState(0). I also edited it now
is there an extra block inside the function passed to getBalance?
hmm still same error: Line 12:5: 'setBalance' is not defined no-undef
|
0

get the balance in useEffect and set it in state:

import { useEffect,useState } from "react";


const [balance, setBallance] = useState(null)

useEffect(() => {
    const loadBalance = async () => {
      // make sure web3 is properly
      const web3 = new Web3(new Web3.providers.HttpProvider('https://****'));
      const balance = await web3.eth.getBalance(addressHere)
      setBallance(web3.utils.fromWei(balance, "ether"))
    }

    loadBalance()
  }, [])

5 Comments

thanks but can you show how to put this in my example?
this works too!! :D he is already doing it just enclose in useEffect
@GaneshBudhathoki No way :)
@Yilmaz by he I was referring to you but okay.
@GaneshBudhathoki I know. i was joking :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.