从 react-web3-provider 访问 web3 属性



我在遵循 react-web3-provider 的基本功能时遇到问题

我像这样围绕组件的根:

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>

但是,我不确定我应该如何从 props 中获取web3,因为在他们的文档之后,没有 props 从父组件传递到子组件。

如何获取web3的实例,就像在:

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

文档中有withWeb3 HOC

import { withWeb3 } from 'react-web3-provider';
class MyComponent {
    render() {
        const { web3 } = this.props;
    }
}
export default withWeb3(MyComponent);

相关内容

  • 没有找到相关文章

最新更新