@@ -268,7 +268,7 @@ Update `src/App.tsx` with the following code:
268
268
269
269
``` ts title="App.tsx"
270
270
import " ./App.css"
271
- import { DiscoverWalletProviders } from " ./components/DiscoverWalletProviders "
271
+ import { DiscoverWalletProviders } from " ./components/WalletProviders "
272
272
273
273
function App() {
274
274
return (
@@ -279,18 +279,17 @@ function App() {
279
279
export default App
280
280
```
281
281
282
- This code renders the ` DiscoverWalletProviders ` component that you'll create in the next step, which
282
+ This code renders the ` WalletProviders ` component that you'll create in the next step, which
283
283
contains the logic for detecting and connecting to wallet providers.
284
284
285
285
#### 4. Detect and connect to wallets
286
286
287
- In the ` src/components ` directory, create a component ` DiscoverWalletProviders.tsx ` with the
288
- following code:
287
+ Create a ` src/components ` directory and add a file ` WalletProviders.tsx ` with the following code:
289
288
290
- ``` ts title="DiscoverWalletProviders .tsx"
289
+ ``` ts title="WalletProviders .tsx"
291
290
import { useState } from " react"
292
291
import { useSyncProviders } from " ../hooks/useSyncProviders"
293
- import { formatAddress } from " ~ /utils"
292
+ import { formatAddress } from " .. /utils"
294
293
295
294
export const DiscoverWalletProviders = () => {
296
295
const [selectedWallet, setSelectedWallet] = useState <EIP6963ProviderDetail >()
@@ -299,15 +298,16 @@ export const DiscoverWalletProviders = () => {
299
298
300
299
// Connect to the selected provider using eth_requestAccounts.
301
300
const handleConnect = async (providerWithInfo : EIP6963ProviderDetail ) => {
302
- try {
303
- const accounts = await providerWithInfo .provider .request ({
304
- method: " eth_requestAccounts"
305
- })
306
-
301
+ const accounts: string [] | undefined =
302
+ await (
303
+ providerWithInfo .provider
304
+ .request ({ method: " eth_requestAccounts" })
305
+ .catch (console .error )
306
+ ) as string [] | undefined ;
307
+
308
+ if (accounts ?.[0 ]) {
307
309
setSelectedWallet (providerWithInfo )
308
310
setUserAccount (accounts ?.[0 ])
309
- } catch (error ) {
310
- console .error (error )
311
311
}
312
312
}
313
313
@@ -363,7 +363,7 @@ address are displayed.
363
363
364
364
#### 5. Add React hooks
365
365
366
- Create a ` src/hooks ` directory and add a ` store.ts ` file with the following code:
366
+ Create a ` src/hooks ` directory and add a file ` store.ts ` with the following code:
367
367
368
368
``` ts title="hooks/store.ts"
369
369
declare global {
0 commit comments