Skip to content

Commit e246bb9

Browse files
authored
Update React Module and Quickstart Steps (#1687)
1 parent 71e6816 commit e246bb9

File tree

4 files changed

+130
-35
lines changed

4 files changed

+130
-35
lines changed

docs/src/routes/docs/[...1]overview/[...2]contribution-guide/+page.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ In order to contribute to the docs, create a PR on the [develop branch](https://
6565

6666
**Important note: The PR template docs checklist must be complete before review can take place.**
6767

68-
PRs for adding/updating a wallet should include a README (new or updated) for the package (located in `docs/src/routes/docs/[...4]wallets`), and adding/updating the module in [docs demo](https://github.com/blocknative/web3-onboard/blob/develop/docs/src/lib/services/onboard.js) and docs package (`docs/package.json`). New injected wallets should also add the wallet to the [natively supported injected wallets list](https://github.com/blocknative/web3-onboard/blob/develop/docs/src/routes/docs/%5B...4%5Dwallets/injected.md).
68+
PRs for adding/updating a wallet should include a README (new or updated) for the package (located in `docs/src/routes/docs/[...4]wallets`), and adding/updating the module in [docs demo](https://github.com/blocknative/web3-onboard/blob/develop/docs/src/lib/services/onboard.js) and docs package (`docs/package.json`). New injected wallets should also add the wallet to the [natively supported injected wallets list](https://github.com/blocknative/web3-onboard/blob/develop/docs/src/routes/docs/wallets/injected.md).
6969

7070
[See here for an example of a docs pull request.](https://github.com/blocknative/web3-onboard/pull/1544/files)
7171

docs/src/routes/docs/[...3]modules/[...3]react/+page.md

Lines changed: 79 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,22 @@ A collection of React hooks for implementing web3-onboard into a React project
1414
<TabPanel value="yarn">
1515

1616
```sh copy
17-
yarn add @web3-onboard/react
17+
yarn add @web3-onboard/react @web3-onboard/injected-wallets
1818
```
1919

2020
</TabPanel>
2121
<TabPanel value="npm">
2222

2323
```sh copy
24-
npm install @web3-onboard/react
24+
npm install @web3-onboard/react @web3-onboard/injected-wallets
2525
```
2626

2727
</TabPanel>
2828
</Tabs>
2929

3030
### Add Code
3131

32-
```javascript
32+
```javascript title="App.js"
3333
import React from 'react'
3434
import { init, useConnectWallet } from '@web3-onboard/react'
3535
import injectedModule from '@web3-onboard/injected-wallets'
@@ -78,6 +78,8 @@ function App() {
7878
</div>
7979
)
8080
}
81+
82+
export default App
8183
```
8284

8385
## Using the `Web3OnboardProvider`
@@ -524,22 +526,82 @@ module.exports = {
524526
}
525527
```
526528

527-
#### If using create-react-app
529+
### If using create-react-app
530+
531+
[CRACO](https://www.npmjs.com/package/@craco/craco) provides a way to override webpack config which is obfuscated in Create React App built applications.
532+
533+
`npm i @craco/craco`
534+
535+
**OR**
536+
537+
`yarn add @craco/craco`
538+
539+
The above webpack 5 example can be used in the `craco.config.js` file at the root level.
540+
541+
```javascript title="craco.config.js"
542+
const webpack = require('webpack')
543+
544+
module.exports = {
545+
webpack: {
546+
configure: {
547+
resolve: {
548+
fallback: {
549+
path: require.resolve('path-browserify')
550+
},
551+
alias: {
552+
assert: 'assert',
553+
buffer: 'buffer',
554+
crypto: 'crypto-browserify',
555+
http: 'stream-http',
556+
https: 'https-browserify',
557+
os: 'os-browserify/browser',
558+
process: 'process/browser',
559+
stream: 'stream-browserify',
560+
util: 'util'
561+
}
562+
},
563+
experiments: {
564+
asyncWebAssembly: true
565+
},
566+
plugins: [
567+
new webpack.ProvidePlugin({
568+
process: 'process/browser',
569+
Buffer: ['buffer', 'Buffer']
570+
})
571+
]
572+
}
573+
}
574+
}
575+
```
528576

529-
[CRACO](https://www.npmjs.com/package/@craco/craco) provides a similar way to override webpack config which is obfuscated in Create React App built applications.
577+
Be sure to update the scripts in package.json:
530578

531-
The above webpack 5 example can be used in the `craco.config.js` file at the root level in this case.
579+
```
580+
"scripts": {
581+
"start": "craco start",
582+
"build": "craco build",
583+
"test": "craco test"
584+
}
585+
```
532586

533587
[React App Rewired](https://www.npmjs.com/package/react-app-rewired) is another option for working with Create React App DApps
534588

589+
Add React App Rewired:
590+
591+
`npm i react-app-rewired`
592+
593+
**OR**
594+
595+
`yarn add react-app-rewired`
596+
535597
Add the following dev dependencies:
536598
`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`
537599

538600
**OR**
539601

540602
`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`
541603

542-
```javascript
604+
```javascript title="config.overrides.js"
543605
const webpack = require('webpack')
544606
const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer')
545607
const path = require('path')
@@ -593,6 +655,16 @@ module.exports = function override(config) {
593655
}
594656
```
595657

658+
Be sure to update the scripts in package.json:
659+
660+
```
661+
"scripts": {
662+
"start": "react-app-rewired start",
663+
"build": "react-app-rewired build",
664+
"test": "react-app-rewired test"
665+
}
666+
```
667+
596668
### Vite
597669

598670
Add the following dev dependencies:

docs/src/routes/examples/[...1]connect-wallet/+page.md

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,32 @@ description: Learn how to connect a wallet to your dapp with Web3-Onboard. For t
1414

1515
<div class="w-full h-5"/>
1616

17+
Remember- if you used create-react-app, please follow the [additional setup instructions](../../docs/modules/react.md#if-using-create-react-app)
18+
1719
<Tabs values={frameworks}>
1820
<TabPanel value="react">
1921

20-
## Step 1: Import + Configure
22+
## Step 1: Install Dependencies
23+
24+
<Tabs values={['yarn', 'npm']}>
25+
<TabPanel value="yarn">
26+
27+
```sh copy
28+
yarn add @web3-onboard/react @web3-onboard/injected-wallets @web3-onboard/infinity-wallet @web3-onboard/fortmatic @web3-onboard/gnosis @web3-onboard/keepkey @web3-onboard/keystone @web3-onboard/ledger @web3-onboard/portis @web3-onboard/trezor @web3-onboard/walletconnect @web3-onboard/coinbase @web3-onboard/magic @web3-onboard/dcent @web3-onboard/sequence @web3-onboard/taho @web3-onboard/trust @web3-onboard/frontier
29+
```
30+
31+
</TabPanel>
32+
<TabPanel value="npm">
33+
34+
```sh copy
35+
npm install @web3-onboard/react @web3-onboard/injected-wallets @web3-onboard/infinity-wallet @web3-onboard/fortmatic @web3-onboard/gnosis @web3-onboard/keepkey @web3-onboard/keystone @web3-onboard/ledger @web3-onboard/portis @web3-onboard/trezor @web3-onboard/walletconnect @web3-onboard/coinbase @web3-onboard/magic @web3-onboard/dcent @web3-onboard/sequence @web3-onboard/taho @web3-onboard/trust @web3-onboard/frontier
36+
```
37+
38+
</TabPanel>
39+
</Tabs>
40+
41+
42+
## Step 2: Import + Configure
2143

2244
Import the libraries and any wallets you would like to use. For this example, we are going to use the injected wallets module. You can easily add more wallet support to your dapp via our other wallet modules. Additionally, we'll setup web3-onboard to support 2 chains: Ethereum mainnet and Polygon mainnet.
2345

@@ -31,17 +53,16 @@ import keepkeyModule from '@web3-onboard/keepkey'
3153
import keystoneModule from '@web3-onboard/keystone'
3254
import ledgerModule from '@web3-onboard/ledger'
3355
import portisModule from '@web3-onboard/portis'
34-
import torusModule from '@web3-onboard/torus'
3556
import trezorModule from '@web3-onboard/trezor'
3657
import walletConnectModule from '@web3-onboard/walletconnect'
3758
import coinbaseModule from '@web3-onboard/coinbase'
3859
import magicModule from '@web3-onboard/magic'
39-
import web3authModule from '@web3-onboard/web3auth'
4060
import dcentModule from '@web3-onboard/dcent'
4161
import sequenceModule from '@web3-onboard/sequence'
4262
import tahoModule from '@web3-onboard/taho'
4363
import trustModule from '@web3-onboard/trust'
4464
import frontierModule from '@web3-onboard/frontier'
65+
import ConnectWallet from './ConnectWallet'
4566

4667
const INFURA_KEY = ''
4768

@@ -79,9 +100,6 @@ const magic = magicModule({
79100
apiKey: 'apiKey'
80101
})
81102

82-
const enkrypt = enkryptModule()
83-
const mewWallet = mewWalletModule()
84-
85103
const wallets = [
86104
infinityWallet,
87105
keepkey,
@@ -95,8 +113,6 @@ const wallets = [
95113
dcent,
96114
trezor,
97115
walletConnect,
98-
enkrypt,
99-
mewWallet,
100116
gnosis,
101117
magic,
102118
fortmatic,
@@ -109,13 +125,13 @@ const chains = [
109125
id: '0x1',
110126
token: 'ETH',
111127
label: 'Ethereum Mainnet',
112-
rpcUrl: `https://mainnet.infura.io/v3/${INFURA_ID}`
128+
rpcUrl: `https://mainnet.infura.io/v3/${INFURA_KEY}`
113129
},
114130
{
115131
id: '0x5',
116132
token: 'ETH',
117133
label: 'Goerli',
118-
rpcUrl: `https://goerli.infura.io/v3/${INFURA_ID}`
134+
rpcUrl: `https://goerli.infura.io/v3/${INFURA_KEY}`
119135
},
120136
{
121137
id: '0x13881',
@@ -167,15 +183,15 @@ function App() {
167183
)
168184
}
169185

170-
export default MyApp
186+
export default App
171187
```
172188

173189
## Step 2: Display the connect wallet button
174190

175191
In another file we'll create the component that will display our connect wallet button. We'll be using the `useConnectWallet` hook in order to achieve this.
176192

177193
```js title="ConnectWallet.tsx"|copy
178-
import { useEffect } from 'react'
194+
import { useEffect, useState } from 'react'
179195
import { useConnectWallet } from '@web3-onboard/react'
180196
import { ethers } from 'ethers'
181197

@@ -209,9 +225,18 @@ export default function ConnectWallet() {
209225
Now that we have our wallet connected, let's display some basic information, such as the connected wallet's address, ENS name, and avatar.
210226
211227
```js title="ConnectWallet.tsx"|copy{8,10-19,28-37}
212-
import { useEffect } from 'react'
228+
import { useEffect, useState } from 'react'
213229
import { useConnectWallet } from '@web3-onboard/react'
214230
import { ethers } from 'ethers'
231+
import type {
232+
TokenSymbol
233+
} from '@web3-onboard/common'
234+
235+
interface Account {
236+
address: string,
237+
balance: Record<TokenSymbol, string> | null,
238+
ens: {name: string|undefined, avatar: string|undefined}
239+
}
215240

216241
export default function ConnectWallet() {
217242
const [{ wallet, connecting }, connect, disconnect] = useConnectWallet()
@@ -238,13 +263,13 @@ export default function ConnectWallet() {
238263
}
239264
}, [wallet])
240265

241-
if(wallet?.provider) {
266+
if(wallet?.provider && account) {
242267
return (
243268
<div>
244-
<img src={ens?.avatar} alt="ENS Avatar" />
245-
<div>{ ens?.name ? ens.name : address }</div>
269+
{account.ens?.avatar ? (<img src={account.ens?.avatar} alt="ENS Avatar" />) : null}
270+
<div>{ account.ens?.name ? account.ens.name : account.address }</div>
246271
<div>Connected to {wallet.label}</div>
247-
<button onClick={() => { disconnect({ label: wallet.label }) }>Disconnect</button>
272+
<button onClick={() => { disconnect({ label: wallet.label }) }}>Disconnect</button>
248273
</div>
249274
)
250275
}
@@ -253,7 +278,7 @@ export default function ConnectWallet() {
253278
<div>
254279
<button
255280
disabled={connecting}
256-
onClick={connect}>
281+
onClick={() => connect()}>
257282
Connect
258283
</button>
259284
</div>

packages/react/README.md

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ A collection of React hooks for implementing web3-onboard in to a React project
1818

1919
### Add Code
2020

21-
```javascript
21+
```javascript title="App.js"
2222
import React from 'react'
2323
import { init, useConnectWallet } from '@web3-onboard/react'
2424
import injectedModule from '@web3-onboard/injected-wallets'
@@ -30,9 +30,8 @@ const apiKey = '1730eff0-9d50-4382-a3fe-89f0d34a2070'
3030

3131
const injected = injectedModule()
3232

33-
// Only one RPC endpoint required per chain
34-
const rpcAPIKey = '<INFURA_KEY>' || '<ALCHEMY_KEY>'
35-
const rpcUrl = `https://eth-mainnet.g.alchemy.com/v2/${rpcAPIKey}` || `https://mainnet.infura.io/v3/${rpcAPIKey}`
33+
const infuraKey = '<INFURA_KEY>'
34+
const rpcUrl = `https://mainnet.infura.io/v3/${infuraKey}`
3635

3736
// initialize Onboard
3837
init({
@@ -55,22 +54,21 @@ function App() {
5554
let ethersProvider
5655

5756
if (wallet) {
58-
ethersProvider = new ethers.providers.Web3Provider(wallet.provider, 'any')
5957
// if using ethers v6 this is:
6058
// ethersProvider = new ethers.BrowserProvider(wallet.provider, 'any')
59+
ethersProvider = new ethers.providers.Web3Provider(wallet.provider, 'any')
6160
}
6261

6362
return (
6463
<div>
65-
<button
66-
disabled={connecting}
67-
onClick={() => (wallet ? disconnect(wallet) : connect())}
68-
>
64+
<button disabled={connecting} onClick={() => (wallet ? disconnect(wallet) : connect())}>
6965
{connecting ? 'connecting' : wallet ? 'disconnect' : 'connect'}
7066
</button>
7167
</div>
7268
)
7369
}
70+
71+
export default App
7472
```
7573

7674
### Using the `Web3OnboardProvider`

0 commit comments

Comments
 (0)