简体   繁体   English

为什么我的变量在带有 web3 的 reactjs 中未定义

[英]why is my variable undefined in reactjs with web3

I am trying to use reactJS to show the balance of an Ethereum wallet with web3.我正在尝试使用 reactJS 来显示带有 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我的代码成功获取余额并将其输出到控制台,但是当我尝试将其分配为变量并在 html 中显示时,我收到以下错误:第 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?我试图用 let 初始化变量,但这会产生更多错误,我该如何解决这个问题?

Try this:尝试这个:

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

export default function balances() {

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

get the balance in useEffect and set it in state:在 useEffect 中获取余额并将其设置在 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()
  }, [])
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.您可以使用 useState 挂钩并在获得该值后分配该值。 If this version doesn't render the correct amount, you can use useEffect hook also如果这个版本没有渲染正确的数量,你也可以使用 useEffect 钩子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM