Ethereum: TypeError: Cannot read properties of undefined (reading ‘Web3Provider’)

I see that you are getting an error related to the Web3Provider component. I will help you fix it.

The problem is that the Web3Provider component expects a provider object to be passed as a support, but in your code you are trying to access it directly from contractABI and contractAddress.

Here is an updated version of your code with the necessary changes:

import React, { useEffect, useState } from "react";

import { ethers } from 'ethers';

import {contract ABI, contractAddress} from '../utils/constants';

const Ethereum = () => {

const [accounts, setAccounts] = useState([]);

useEffect(() => {

// Set the provider and get the accounts

const provider = new ethers.providers.Web3Provider(window.ethereum);

const accountsList = await provider.get signedAccounts();

setAccounts(accountsList);

// If no accounts are found, try connecting to the Ethereum network manually

if (accounts.length === 0) {

console.log("No accounts found.");

}

}, []);

return (


Ethereum







Ethereum: TypeError: Cannot read properties of undefined (reading 'Web3Provider')

{/ Your React app content here /}

);

};

Ethereum default export;

In this updated version, I added a useState hook to store the list of accounts. When the component mounts, it tries to connect to the Ethereum network using the provider and gets the signed accounts. If no accounts are found, it logs a message.

The main difference is that we now pass a provider object to support the Ethereum component, instead of trying to access it directly from contractABI and contractAddress.

Make sure to install ethers.js by running npm install ethers or yarn add ethers in your project directory.

Leave a Reply

Your email address will not be published. Required fields are marked *