0

Blockchain newbie.

If I place the contract JS in my React App, MetaMask always starts when the Dapp loads.

import donboard from "./donboard";

However, if I remove the import line, I can rely on the below code to help me connect with MetaMask when user clicks the button.

onSubmit = async (event) => {
event.preventDefault();
if (window.ethereum) {
  window.web3 = new Web3(window.ethereum);
  try {
    window.ethereum.enable();
    const accounts = await window.ethereum.request({ method: 'eth_accounts' });
    const account = accounts[0];
    this.setState({ account });
    console.log("async onSubmit: " + account);
  } catch (err) {
    console.log("not connected-onSubmit: " + err);
  }
}};

As I will need the contract JS once user is connected, how can I resolve this problem?

Thanks.

1 Answer 1

0

I resolved this issue by not exporting the contract

export default new web3.eth.Contract(abi, address);

, but just bringing the loading of the ABI and address into a conditional when the required button is pressed.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.