|
| 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. |
0 commit comments