简体   繁体   中英

Web3 version not same everywhere

I am initializing web3 from metamask ectension in react this way-

componentDidMount() {
     window.addEventListener('load', function() {
        let web3 = window.web3
        web3 = new Web3(web3.currentProvider);
        console.log(web3.version)
     })
}

Above console.log give version 1.0.0-beta.22 , however when I call web3.version inside of other function inside the component-

handleChange = (e) => {
    let web3 = window.web3
    console.log(web3.version)
};

Here console.log(web3.version) gives version 0.20.2

Web3 version I have installed using npm is version 1.0.0-beta.22 . I guess 0.20.2 version is injected by metamask somehow. I want to use version 1.0.0-beta.22 everywhere. How can I do that?

Turns out I was doing a silly mistake. Solution-

componentDidMount() {
 window.addEventListener('load', function() {
    let web3 = window.web3
    window.web3 = new Web3(web3.currentProvider);
    console.log(web3.version)
 })

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM