Last active
February 15, 2018 07:46
-
-
Save adrianmcli/56f2d90159feddfcd2574d50b94b8978 to your computer and use it in GitHub Desktop.
React.js HOC for Web3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react' | |
import getWeb3 from './getWeb3' | |
const withWeb3 = PassedComponent => class extends React.Component { | |
state = { web3: null } | |
async componentDidMount () { | |
try { | |
const web3 = await getWeb3() | |
this.setState({ web3 }) | |
} catch (error) { | |
alert(`Failed to load web3.`) | |
console.log(error) | |
} | |
} | |
render () { | |
const { web3 } = this.state | |
const Loading = <div>Loading Web3</div> | |
return web3 ? <Loading /> : <PassedComponent web3={web3} /> | |
} | |
} | |
export default withWeb3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment