Modules
React
A collection of React hooks for implementing web3-onboard into a React project
Quickstart with Injected Wallets and Ethers Provider
Install
- Yarn
- Npm
Add Code
Web3OnboardProvider
Using the You can use the context provider Web3OnboardProvider
to better manage global state. Simply wrap the provider around your App
and
the initialized web3Onboard instance will be available in all children components. See example below.
init
The init
function must be called before any hooks can be used. The init
function just initializes web3-onboard
and makes it available for all hooks to use. For reference check out the
initialization docs for @web3-onboard/core
useConnectWallet
This hook allows you to connect the user's wallet and track the state of the connection status and the wallet that is connected.
setPrimaryWallet
The primary wallet (first in the list of connected wallets) and primary account (first in the list of connected accounts for a wallet) can be set by using the setPrimaryWallet
function. The wallet that is set needs to be passed in for the first parameter and if you would like to set the primary account, the address of that account also needs to be passed in:
useSetChain
This hook allows you to set the chain of a user's connected wallet, keep track of the current chain the user is connected to and the status of setting the chain. Passing in a wallet label will operate on that connected wallet, otherwise it will default to the last connected wallet. If a chain was instantiated without an rpcUrl, token, or label, add these options for wallets that require this information for adding a new chain.
useNotifications
This hook allows the dev to access all notifications if enabled, send custom notifications and update notify <enable/disable & update transactionHandler function>
note requires an API key be added to the initialization, enabled by default if API key exists
For full Notification documentation please see
Notify section within the @web3-onboard/core
docs
useWallets
This hook allows you to track the state of all the currently connected wallets.
useAccountCenter
This hook allows you to track and update the state of the AccountCenter
useSetLocale
This hook allows you to set the locale of your application to allow language updates associated with the i18n config
Build Environments
Many of the wallet modules require dependencies that are not normally included in browser builds (namely the node built-in modules such as crypto
, buffer
, util
etc). If you are having build issues you can try the following bundler configs to resolve these dependency issues:
Webpack 4
Node built-ins are automatically bundled in v4 so that portion is handled automatically.
web3auth and torus will require a Babel to compile from es6 if not already supported. See config for Babel and Webpack4 as follows
npm i --save-dev @babel/cli @babel/core @babel/node @babel/plugin-proposal-nullish-coalescing-operator @babel/plugin-proposal-optional-chaining @babel/plugin-syntax-bigint @babel/register
AND
npm i babel-loader
babel.config.js
webpack.config.js
Webpack 5
You'll need to add some dev dependencies with the following command:
npm i --save-dev assert buffer crypto-browserify stream-http https-browserify os-browserify process stream-browserify util path-browserify browserify-zlib
Then add the following to your webpack.config.js
file:
If using create-react-app
CRACO provides a similar way to override webpack config which is obfuscated in Create React App built applications.
The above webpack 5 example can be used in the craco.config.js
file at the root level in this case.
React App Rewired is another option for working with Create React App DApps
Add the following dev dependencies:
npm i --save-dev rollup-plugin-polyfill-node webpack-bundle-analyzer assert buffer crypto-browserify stream-http https-browserify os-browserify process stream-browserify util path-browserify browserify-zlib
OR
yarn add rollup-plugin-polyfill-node webpack-bundle-analyzer assert buffer crypto-browserify stream-http https-browserify os-browserify process stream-browserify util path-browserify browserify-zlib -D
Vite
Add the following dev dependencies:
npm i --save-dev rollup-plugin-polyfill-node crypto-browserify stream-browserify assert
Then add the following to your vite.config.js
file: