Skip to content

Commit 3bebeb9

Browse files
Fix React TS connect guide (Wallet API) (#1995)
1 parent 4a16ee4 commit 3bebeb9

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

wallet/how-to/connect.md

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ Update `src/App.tsx` with the following code:
268268

269269
```ts title="App.tsx"
270270
import "./App.css"
271-
import { DiscoverWalletProviders } from "./components/DiscoverWalletProviders"
271+
import { DiscoverWalletProviders } from "./components/WalletProviders"
272272

273273
function App() {
274274
return (
@@ -279,18 +279,17 @@ function App() {
279279
export default App
280280
```
281281

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
283283
contains the logic for detecting and connecting to wallet providers.
284284

285285
#### 4. Detect and connect to wallets
286286

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:
289288

290-
```ts title="DiscoverWalletProviders.tsx"
289+
```ts title="WalletProviders.tsx"
291290
import { useState } from "react"
292291
import { useSyncProviders } from "../hooks/useSyncProviders"
293-
import { formatAddress } from "~/utils"
292+
import { formatAddress } from "../utils"
294293

295294
export const DiscoverWalletProviders = () => {
296295
const [selectedWallet, setSelectedWallet] = useState<EIP6963ProviderDetail>()
@@ -299,15 +298,16 @@ export const DiscoverWalletProviders = () => {
299298

300299
// Connect to the selected provider using eth_requestAccounts.
301300
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]) {
307309
setSelectedWallet(providerWithInfo)
308310
setUserAccount(accounts?.[0])
309-
} catch (error) {
310-
console.error(error)
311311
}
312312
}
313313

@@ -363,7 +363,7 @@ address are displayed.
363363

364364
#### 5. Add React hooks
365365

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:
367367

368368
```ts title="hooks/store.ts"
369369
declare global {

0 commit comments

Comments
 (0)