Package Exports
- react-web3-provider
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (react-web3-provider) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
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 RPC fallback (e.g. INFURA node).
Ready for the upcoming changes in MetaMask.
Installation
$ yarn add react-web3-providerBasic usage
Add the Web3Provider to your root React component:
import Web3Provider from 'react-web3-provider';
ReactDOM.render(
<Web3Provider
defaultWeb3Provider="https://mainnet.infura.io/YOUR_API_KEY"
loading="Loading..."
error={(err) => `Connection error: ${err.message}`}
>
<App />
</Web3Provider>
)Then in component where you want to use Web3:
import { withWeb3 } from 'react-web3-provider';
class MyComponent {
render() {
const { web3 } = this.props;
web3.eth.getAccounts(console.log);
// Version 1.0.0-beta.35
return "Web3 version: {web3.version}";
}
}
export default withWeb3(MyComponent);Custom web3 state handling
You can render the web3 state somewhere else in the page instead of the global loading and error components:
import Web3Provider from 'react-web3-provider';
ReactDOM.render(
<Web3Provider
defaultWeb3Provider="https://mainnet.infura.io/YOUR_API_KEY"
>
<App />
</Web3Provider>
)You can use the injected web3State property in your components:
import { withWeb3 } from 'react-web3-provider';
class MyComponent {
render() {
const { web3, web3State } = this.props;
return (
<pre>
{web3State.isConnected && "Connected!\n"}
{web3State.isLoading && "Loading...\n"}
{web3State.error && `Connection error: ${web3State.error.message}\n`}
Web3 version: {web3.version}
</pre>
);
}
}
export default withWeb3(MyComponent);