Skip to content

Commit 4b4ad40

Browse files
Adds nextjs v13 working example (#1411)
* Adds nextjs v13 working example * Minor tweaks * Remove .vercel folder * Add comments
1 parent 8a9d642 commit 4b4ad40

File tree

15 files changed

+7097
-1
lines changed

15 files changed

+7097
-1
lines changed

examples/with-nextjs-13/.gitignore

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2+
3+
# dependencies
4+
/node_modules
5+
/.pnp
6+
.pnp.js
7+
8+
# testing
9+
/coverage
10+
11+
# next.js
12+
/.next/
13+
/out/
14+
15+
# production
16+
/build
17+
18+
# misc
19+
.DS_Store
20+
*.pem
21+
22+
# debug
23+
npm-debug.log*
24+
yarn-debug.log*
25+
yarn-error.log*
26+
.pnpm-debug.log*
27+
28+
# local env files
29+
.env*.local
30+
31+
# vercel
32+
.vercel
33+
34+
# typescript
35+
*.tsbuildinfo
36+
next-env.d.ts

examples/with-nextjs-13/README.md

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# Web3-Onboard + NextJs
2+
3+
4+
5+
This should serve as a reference example for how to integrate Web3-Onboard with a NextJs app! 🙌
6+
7+
The [web3-onboard.ts](./web3-onboard.ts) file contains everything necessary to configure and initialize Web3-Onboard.
8+
9+
Here's the TL;DR
10+
11+
1. Wrap the provider
12+
13+
[_app.tsx](./pages/_app.tsx)
14+
```react
15+
export default function App({ Component, pageProps }: AppProps) {
16+
return (
17+
<Web3OnboardProvider web3Onboard={web3Onboard}>
18+
<Component {...pageProps} />
19+
</Web3OnboardProvider>
20+
)
21+
}
22+
```
23+
24+
2. Import and Setup
25+
26+
[index.tsx](./pages/index.tsx)
27+
```ts
28+
// ...
29+
export default function Home() {
30+
const [{ wallet, connecting }, connect, disconnect] = useConnectWallet()
31+
32+
// create an ethers provider
33+
let ethersProvider
34+
35+
if (wallet) {
36+
ethersProvider = new ethers.providers.Web3Provider(wallet.provider, 'any')
37+
}
38+
39+
return (/* ... */)
40+
}
41+
```
42+
43+
3. Add the wallet connect button
44+
45+
[index.tsx](./pages/index.tsx)
46+
```html
47+
<button
48+
style={buttonStyles}
49+
disabled={connecting}
50+
onClick={() => (wallet ? disconnect(wallet) : connect())}
51+
>
52+
{connecting ? 'Connecting' : wallet ? 'Disconnect' : 'Connect'}
53+
</button>
54+
```
55+
56+
And that's it! 🎉 You should be well on your way to **buidl**ing that next killer dApp!
57+
58+
---
59+
60+
This is a [Next.js](https://nextjs.org/) project bootstrapped with [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app).
61+
62+
## Getting Started
63+
64+
First, run the development server:
65+
66+
```bash
67+
npm run dev
68+
# or
69+
yarn dev
70+
```
71+
72+
Open [http://localhost:3000](http://localhost:3000) with your browser to see the result.
73+
74+
You can start editing the page by modifying `pages/index.tsx`. The page auto-updates as you edit the file.
75+
76+
[API routes](https://nextjs.org/docs/api-routes/introduction) can be accessed on [http://localhost:3000/api/hello](http://localhost:3000/api/hello). This endpoint can be edited in `pages/api/hello.ts`.
77+
78+
The `pages/api` directory is mapped to `/api/*`. Files in this directory are treated as [API routes](https://nextjs.org/docs/api-routes/introduction) instead of React pages.
79+
80+
## Learn More
81+
82+
To learn more about Next.js, take a look at the following resources:
83+
84+
- [Next.js Documentation](https://nextjs.org/docs) - learn about Next.js features and API.
85+
- [Learn Next.js](https://nextjs.org/learn) - an interactive Next.js tutorial.
86+
87+
You can check out [the Next.js GitHub repository](https://github.com/vercel/next.js/) - your feedback and contributions are welcome!
88+
89+
## Deploy on Vercel
90+
91+
The easiest way to deploy your Next.js app is to use the [Vercel Platform](https://vercel.com/new?utm_medium=default-template&filter=next.js&utm_source=create-next-app&utm_campaign=create-next-app-readme) from the creators of Next.js.
92+
93+
Check out our [Next.js deployment documentation](https://nextjs.org/docs/deployment) for more details.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/** @type {import('next').NextConfig} */
2+
const nextConfig = {
3+
reactStrictMode: true,
4+
swcMinify: true,
5+
}
6+
7+
module.exports = nextConfig
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"name": "with-nextjs-13",
3+
"version": "0.1.0",
4+
"private": true,
5+
"scripts": {
6+
"dev": "next dev",
7+
"build": "next build",
8+
"start": "next start",
9+
"lint": "next lint"
10+
},
11+
"dependencies": {
12+
"@types/node": "18.11.11",
13+
"@types/react": "18.0.26",
14+
"@types/react-dom": "18.0.9",
15+
"@web3-onboard/coinbase": "^2.1.3",
16+
"@web3-onboard/dcent": "^2.2.1",
17+
"@web3-onboard/fortmatic": "^2.0.14",
18+
"@web3-onboard/gnosis": "^2.1.3",
19+
"@web3-onboard/injected-wallets": "^2.2.4",
20+
"@web3-onboard/keepkey": "^2.3.1",
21+
"@web3-onboard/keystone": "^2.3.1",
22+
"@web3-onboard/ledger": "^2.3.1",
23+
"@web3-onboard/magic": "^2.1.3",
24+
"@web3-onboard/mew-wallet": "^2.0.0",
25+
"@web3-onboard/portis": "^2.1.3",
26+
"@web3-onboard/react": "^2.3.5",
27+
"@web3-onboard/sequence": "^2.0.3-alpha.1",
28+
"@web3-onboard/tallyho": "^2.0.1",
29+
"@web3-onboard/torus": "^2.1.3",
30+
"@web3-onboard/trezor": "^2.3.1",
31+
"@web3-onboard/walletconnect": "^2.1.3",
32+
"ethers": "^5.7.2",
33+
"next": "13.0.6",
34+
"react": "18.2.0",
35+
"react-dom": "18.2.0",
36+
"typescript": "4.9.4"
37+
}
38+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import '../styles/globals.css'
2+
import type { AppProps } from 'next/app'
3+
import web3Onboard from '../web3-onboard'
4+
import { Web3OnboardProvider } from '@web3-onboard/react'
5+
6+
export default function App({ Component, pageProps }: AppProps) {
7+
return (
8+
<Web3OnboardProvider web3Onboard={web3Onboard}>
9+
<Component {...pageProps} />
10+
</Web3OnboardProvider>
11+
)
12+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
2+
import type { NextApiRequest, NextApiResponse } from 'next'
3+
4+
type Data = {
5+
name: string
6+
}
7+
8+
export default function handler(
9+
req: NextApiRequest,
10+
res: NextApiResponse<Data>
11+
) {
12+
res.status(200).json({ name: 'John Doe' })
13+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import Head from 'next/head'
2+
import Image from 'next/image'
3+
import styles from '../styles/Home.module.css'
4+
5+
import { useConnectWallet } from '@web3-onboard/react'
6+
import { ethers } from 'ethers'
7+
8+
const buttonStyles = {
9+
borderRadius: '6px',
10+
background: '#111827',
11+
border: 'none',
12+
fontSize: '18px',
13+
fontWeight: '600',
14+
cursor: 'pointer',
15+
color: 'white',
16+
padding: '14px 12px',
17+
marginTop: '40px',
18+
fontFamily: 'inherit'
19+
}
20+
21+
export default function Home() {
22+
const [{ wallet, connecting }, connect, disconnect] = useConnectWallet()
23+
24+
// create an ethers provider
25+
let ethersProvider
26+
27+
if (wallet) {
28+
ethersProvider = new ethers.providers.Web3Provider(wallet.provider, 'any')
29+
}
30+
31+
return (
32+
<div className={styles.container}>
33+
<Head>
34+
<title>Create Next App</title>
35+
<meta name="description" content="Generated by create next app" />
36+
<link rel="icon" href="/favicon.ico" />
37+
</Head>
38+
39+
<main className={styles.main}>
40+
<h1 className={styles.title}>
41+
Welcome to this demo of{' '}
42+
<a href="https://onboard.blocknative.com"> Web3-Onboard!</a>
43+
</h1>
44+
45+
<button
46+
style={buttonStyles}
47+
disabled={connecting}
48+
onClick={() => (wallet ? disconnect(wallet) : connect())}
49+
>
50+
{connecting ? 'Connecting' : wallet ? 'Disconnect' : 'Connect'}
51+
</button>
52+
</main>
53+
54+
<footer className={styles.footer}>
55+
<a
56+
href="https://vercel.com?utm_source=create-next-app&utm_medium=default-template&utm_campaign=create-next-app"
57+
target="_blank"
58+
rel="noopener noreferrer"
59+
>
60+
Powered by{' '}
61+
<span className={styles.logo}>
62+
<Image src="/vercel.svg" alt="Vercel Logo" width={72} height={16} />
63+
</span>
64+
</a>
65+
</footer>
66+
</div>
67+
)
68+
}
25.3 KB
Binary file not shown.
Lines changed: 4 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)