0.2.0 • Published 6 years ago
@dapperlabs/react-web3 v0.2.0
react-web3?
Context provider for interacting with injected web3.
values provided on context:
web3: the injected web3 instancecheckingForWeb3: boolean indicating whether still actively waiting for injection.account: current user wallet addressnetwork: current network idrequestAccounts: utility method for requesting the users accountsgetAccounts: utility method for accessing the user accountspersonalSign: utility method for performing a personal sign
Setup
Import the provider and wrap your application in it.
import { Web3Provider } from '@dapperlabs/react-web3'
function Root() {
return (
<Web3Provider>
<MyDApp />
</Web3Provider>
)
}Context
Context is directly available via import { Web3Context } from '@dapperlabs/react-web3 and can be consumed however you'd like:
useContext(Web3Context)static contextType = Web3Context<Web3Context.Consumer />
HOC (withWeb3)
Decorates your component with the above context values spread onto it.
\
Utility component to drop into your app that will render the current values of the context properties.
import { Web3Toolbar } ...
function MyDapp() {
return (
<div>
{process.env.NODE_ENV !== 'production' && (
<Web3Toolbar />
)}
<OtherStuff />
</div>
)
}Hooks
useInjectedWeb3()useWeb3Status( true/false )where the parameter indicates whether to poll for the status.
function () {
const [web3, checkingForWeb3] = useInjectedWeb3();
const [account, network] = useWeb3Status();
...
}