从react-web3-provider访问web3 prop

时间:2019-01-07 18:42:08

标签: javascript reactjs web3

我在遵循[react-web3-provider] [1]的基本功能时遇到麻烦

我像这样围绕组件的根:

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>

但是,我不确定应该如何从道具中获取web3,因为按照其文档说明,没有道具从父组件传递到子组件。

如何获取web3的实例,如下所示:

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

1 个答案:

答案 0 :(得分:0)

文档中有print个HOC

withWeb3