How Can I pass value from useState into balanceOf function in web3

David Jay

I'm creating a DEFI dapp, I want to get the account that is connected with the meta mask using web3.eth.get accounts();

This is what I got when console.log ['0x32F36b36e78E89bdd6efa9e893ec2d87e4E2e3E9'], I've created use State to push this account to use in React components as props but when I console.log the value of useState I got nothing.

Then if I will get this value inside useState how can I pass it into the balanceOf function?

useState

const [userAccount, setUserAccount] = useState([]);

the function to get the accounts and get the balance of this account

const blockchainDataLoad = async () => {

const web3 = window.web3;
console.log(userAccount);
// get the Accounts
const accounts = await web3.eth.getAccounts();
setUserAccount(accounts[0]);




// get the Network Id of The contracts
const networkId = await web3.eth.net.getId();

// ******************************** Dai Token EuroToken ************************* //

// Get the deployed contract object using the network ID
const daiTokenData = DaiToken.networks[networkId];

if(daiTokenData) {

  // Create a instance of the contract
  const daiToken = new web3.eth.Contract(DaiToken.abi, daiTokenData.address);
  setDTokens(daiToken);

  // get the balance from the investor account
  **let daiTokenBalance = await daiToken.methods.balanceOf(userAccount).call();**
  setDTokenBalance(daiTokenBalance.toString());
  console.log(daiTokenBalance);
Yilmaz

You should get the accounts in useEffect hook, so when your component renders, your component's state will be already set.

useEffect(() => {
    const getAccount = async () => {
      const accounts = await web3.eth.getAccounts();
      setAccount(accounts[0]);
    };
    // call getAccount() if web3 is defined otherwise you app will break
    web3 && getAccount();
  }, [web3]);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I pass a keypress value to a function, through an event listener?

How can I pass a value from a Plone content schema field into a JQuery function?

How Can I Pass the Value of A Textfield to a Button Clicked Function in Swift?

How do I pass value from one function to another?

How can I pass a property value from a function to a 'modal' dialog in React?

React: How can I pass a value to my main App() function

How can I get the value of a async and pass to a function?

How can I pass a value into a variadic template function that forwards the value into a function that receives the value by copy?

How do I pass parameters in a function with useState?

How can i pass the jquery value to php function

How can I always pass the current variable value into a python function?

How Can I Pass the Value of A Textfield to a Button Clicked Function in Swift3?

How I can pass multiple array in value in PowerShell function

how can i pass value from javascript to my php function codeigniter

How can I pass a function parameter as a switch statement case value?

How to pass function as initial value of an object inside useState()?

how can i resolve this error while installing web3

How can i pass useState value from child to parent

Why can't I pass a prop from useState to another component?

How can I pass an alias value to a bazel function?

How can I update an array using useState, with as many numbers as the ones I pass to the function?

How to pass an useState to a Function from a Funtional Component?

How can i access the data from one useState in the next useState

How can i update a useState from an array?

How can I pass a value from a function to any component in React?

[Question]How i can show Cid (https) from web3 storage at website?

Can anyone suggest a idea on how do I pass the value from my useState (toDoData1 and toDoData2) to MapRegion (latitude and longitude)

How to pass bytes type parameters to a function in java web3

How can I use a fresh useState value in a function called from a looped function including a timeout in React?