Skip to content

Instantly share code, notes, and snippets.

@adrianmcli
Last active February 15, 2018 07:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adrianmcli/56f2d90159feddfcd2574d50b94b8978 to your computer and use it in GitHub Desktop.
Save adrianmcli/56f2d90159feddfcd2574d50b94b8978 to your computer and use it in GitHub Desktop.
React.js HOC for Web3
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