Skip to content

Commit 1abe6b6

Browse files
Merge pull request #1216 from blocknative/feature/context-provider-react
Adds React Context Provider
2 parents 26c3464 + 01f7afd commit 1abe6b6

26 files changed

+2370
-192
lines changed

examples/with-nextjs/.eslintrc.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": ["next/babel", "next/core-web-vitals"]
3+
}

examples/with-nextjs/.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/README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
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).
2+
3+
## Getting Started
4+
5+
First, run the development server:
6+
7+
```bash
8+
npm run dev
9+
# or
10+
yarn dev
11+
```
12+
13+
Open [http://localhost:3000](http://localhost:3000) with your browser to see the result.
14+
15+
You can start editing the page by modifying `pages/index.js`. The page auto-updates as you edit the file.
16+
17+
[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.js`.
18+
19+
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.
20+
21+
## Learn More
22+
23+
To learn more about Next.js, take a look at the following resources:
24+
25+
- [Next.js Documentation](https://nextjs.org/docs) - learn about Next.js features and API.
26+
- [Learn Next.js](https://nextjs.org/learn) - an interactive Next.js tutorial.
27+
28+
You can check out [the Next.js GitHub repository](https://github.com/vercel/next.js/) - your feedback and contributions are welcome!
29+
30+
## Deploy on Vercel
31+
32+
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.
33+
34+
Check out our [Next.js deployment documentation](https://nextjs.org/docs/deployment) for more details.

examples/with-nextjs/next.config.js

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

examples/with-nextjs/package.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "with-nextjs",
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+
"@web3-onboard/react": "2.2.6-alpha.1",
13+
"next": "12.2.5",
14+
"react": "18.2.0",
15+
"react-dom": "18.2.0"
16+
},
17+
"devDependencies": {
18+
"eslint": "8.22.0",
19+
"eslint-config-next": "12.2.5"
20+
}
21+
}

examples/with-nextjs/pages/_app.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import '../styles/globals.css'
2+
import { Web3OnboardProvider, init } from '@web3-onboard/react'
3+
import injectedModule from '@web3-onboard/injected-wallets'
4+
5+
const INFURA_KEY = ''
6+
7+
const ethereumRopsten = {
8+
id: '0x3',
9+
token: 'rETH',
10+
label: 'Ethereum Ropsten',
11+
rpcUrl: `https://ropsten.infura.io/v3/${INFURA_KEY}`
12+
}
13+
14+
const polygonMainnet = {
15+
id: '0x89',
16+
token: 'MATIC',
17+
label: 'Polygon',
18+
rpcUrl: 'https://matic-mainnet.chainstacklabs.com'
19+
}
20+
21+
const chains = [ethereumRopsten, polygonMainnet]
22+
const wallets = [injectedModule()]
23+
24+
const web3Onboard = init({
25+
wallets,
26+
chains,
27+
appMetadata: {
28+
name: "Web3-Onboard Demo",
29+
icon: '<svg>My App Icon</svg>',
30+
description: "A demo of Web3-Onboard."
31+
}
32+
})
33+
34+
function MyApp({ Component, pageProps }) {
35+
return (
36+
<Web3OnboardProvider web3Onboard={web3Onboard}>
37+
<Component {...pageProps} />
38+
</Web3OnboardProvider>
39+
)
40+
}
41+
42+
export default MyApp
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
2+
3+
export default function handler(req, res) {
4+
res.status(200).json({ name: 'John Doe' })
5+
}

examples/with-nextjs/pages/index.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import Head from 'next/head'
2+
import styles from '../styles/Home.module.css'
3+
4+
import { useConnectWallet } from '@web3-onboard/react'
5+
import { ethers } from 'ethers'
6+
7+
const buttonStyles = {
8+
borderRadius: '6px',
9+
background: '#111827',
10+
border: 'none',
11+
fontSize: '18px',
12+
fontWeight: '600',
13+
cursor: 'pointer',
14+
color: 'white',
15+
padding: '14px 12px',
16+
marginTop: '40px',
17+
fontFamily: 'inherit'
18+
}
19+
20+
export default function Home() {
21+
const [{ wallet, connecting }, connect, disconnect] = useConnectWallet()
22+
23+
// create an ethers provider
24+
let ethersProvider
25+
26+
if (wallet) {
27+
ethersProvider = new ethers.providers.Web3Provider(wallet.provider, 'any')
28+
}
29+
30+
return (
31+
<div className={styles.container}>
32+
<Head>
33+
<title>Web3-Onboard Demo</title>
34+
<meta
35+
name="description"
36+
content="Example of how to integrate Web3-Onboard with Next.js"
37+
/>
38+
<link rel="icon" href="/favicon.ico" />
39+
</Head>
40+
41+
<main className={styles.main}>
42+
<h1 className={styles.title}>
43+
Welcome to this demo of
44+
<a href="https://github.com/blocknative/web3-onboard">
45+
{' '}
46+
Web3-Onboard
47+
</a>
48+
!
49+
</h1>
50+
<button
51+
style={buttonStyles}
52+
disabled={connecting}
53+
onClick={() => (wallet ? disconnect(wallet) : connect())}
54+
>
55+
{connecting ? 'Connecting' : wallet ? 'Disconnect' : 'Connect'}
56+
</button>
57+
</main>
58+
</div>
59+
)
60+
}
25.3 KB
Binary file not shown.
Lines changed: 4 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)