2

I currently have my app set up with testrpc and I'm trying to set the current provider to Rinkeby (or whatever testnet I'm on in MetaMask). However, when I run the following code, nothing shows up. I'm calling initWeb3 in my componentWillMount.

initWeb3() {
if (typeof web3 !== 'undefined') {
    window.web3 = new Web3(web3.currentProvider);
} else {
    console.log('No web3? You should consider trying MetaMask!')
    window.web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
}
1

A bit old question, however, if someone came up with the issue, you need to init it with load event.

componentWillMount() {
   window.addEventListener('load', this.initWeb3)
}

componentWillUnmount() {
   window.removeEventListener('load', this.initWeb3)
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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