react-web3-provider
Simple higher-order component (HOC) providing a web3 context to React app.
Detects whether the user is using MetaMask or Ethereum wallet-enabled browser. If not, it will access the Ethereum network through a given Web3 fallback provider (e.g. INFURA node).
Ready for the upcoming changes in MetaMask.
Installation
$ yarn add react-web3-provider
Basic usage
Add the Web3Provider
to your root React component:
;; ReactDOM
Then in component where you want to use Web3:
; { const web3 = thisprops; web3eth; // Version 1.0.0-beta.35 return "Web3 version: {web3.version}"; } MyComponent;
Custom web3 state handling
You can render the web3 state somewhere else in the page instead of the global loading
and error
components:
;; ReactDOM
You can use the injected web3State
property in your components:
; { const web3 web3State = thisprops; return <pre> web3StateisConnected && "Connected!\n" web3StateisLoading && "Loading...\n" web3Stateerror && `Connection error: \n` Web3 version: web3version </pre> ; } MyComponent;
Web3 Provider filtering
It may be useful to skip the MetaMask Provider if the user has the MetaMask extension installed but is currently not signed-in. We can use acceptProvider
parameter to filter out Web3 Provider. The given defaultProvider
is always accepted.
ReactDOM;
Hooked wallet
More complex example demonstrating transaction sending with a zero-client wallet.
;;;;;;;; const defaultWeb3Provider = { // Light-wallet options const vaultOpts = seedPhrase: '...' password: '...' hdPathString: "m/44'/60'/0'/0" const lightWalletEnabled = true; ;} ReactDOM;
Sending transaction:
;; { const web3 = thisprops; ; } { return <button onClick= this>SEND TRANSACTION</button>; }
Contributors
- Peter