Skip to main content

Command Palette

Search for a command to run...

Connecting MetaMask's web3 to a React app

Published
1 min read
A
I am a web developer from Navi Mumbai. Mainly dealt with LAMP stack, now into Django and getting into Laravel and Cloud. Founder of nerul.in and gaali.in

If you want your React app to connect to MetaMask's web3, const web3 = new Web3(window.web3.currentProvider);will no longer work as suggested. We need to MetaMask to authorize the connection which is a manual process by clicking on a button to authorize like OAuth kind of process.

npm install web3

import Web3 from 'web3';

// This alone won't work anymore
// const web3 = new Web3(window.web3.currentProvider);

// Connect to metamask
window.ethereum.request({ method: 'eth_requestAccounts' });

const web3 = new Web3(Web3.givenProvider || 'ws://localhost:8545');

export default web3;

Thanks to Brian Marquis : https://www.udemy.com/user/brianmarquis/

74 views