简体   繁体   中英

Accessing web3 prop from react-web3-provider

I am having trouble following the basic functionality of [react-web3-provider][1]

I am surrounding the root of the component like this:

 import React, { Component } from "react" import { withWeb3 } from 'react-web3-provider'; import Web3 from 'web3'; import Web3Provider from 'react-web3-provider'; class App extends Component { render() { return ( < Web3Provider defaultProvider = { (cb) => cb(new Web3(new Web3.providers.HttpProvider("https://mainnet.infura.io/YOUR_API_KEY"))) } loading = "Loading..." error = { (err) => `Connection error: ${err.message}` } > <div className = "App" > <NavBar / > </div> </ Web3Provider > ) } } export const NavBar = props => { const { web3 } = props return ( <nav> <ul> <li> {" "} <Link> Partial f </Link>{" "} </li> <li> {" "} <Text.span> Wallet Address: {web3.eth.wallet[0]} </Text.span>{" "} </li> </ul> </nav> ) } export default withWeb3(NavBar) 
 <script src="https://github.com/ethereum/web3.js/blob/develop/lib/web3.js"></script> <script src="https://github.com/hussy-io/react-web3-provider/blob/master/src/index.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script> 

However, I am not sure how I am supposed to get web3 from props, as following their documentation, there are no props are passed down from the parent to the child component.

How can I get the instance of web3 , just as in:

 class MyComponent {
    render() {
        const { web3 } = this.props;
....

There is withWeb3 HOC in documentation

import { withWeb3 } from 'react-web3-provider';

class MyComponent {
    render() {
        const { web3 } = this.props;

    }
}

export default withWeb3(MyComponent);

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