@@ -414,15 +414,26 @@ Now let's connect a frontend.
414414
415415## Create a React App
416416
417- Create a React app with Flow integration:
417+ Create a Next.js app with Flow integration:
418+
419+ ``` bash
420+ npx create-next-app@latest flow-fork-app
421+ ```
422+
423+ During setup, choose:
424+
425+ - ** Use TypeScript** : Yes
426+ - ** Use src directory** : Yes
427+ - ** Use App Router** : Yes
428+
429+ Then install the Flow React SDK:
418430
419431``` bash
420- npx create-react-app flow-fork-app
421432cd flow-fork-app
422433npm install @onflow/react-sdk
423434```
424435
425- Copy your project's ` flow.json ` into the React app's ` src ` directory:
436+ Copy your project's ` flow.json ` into the app's ` src ` directory:
426437
427438``` bash
428439# From your flow-fork-app directory
@@ -431,18 +442,28 @@ cp ../flow.json src/
431442
432443This allows the ` FlowProvider ` to resolve contract imports.
433444
434- Replace ` src/index.js ` with:
445+ ### Configure for Fork Testing
446+
447+ Since Next.js uses the App Router with server components, create a client component wrapper. First, create the components directory:
448+
449+ ``` bash
450+ mkdir -p src/components
451+ ```
452+
453+ Then create ` src/components/FlowProviderWrapper.tsx ` :
454+
455+ ``` typescript
456+ ' use client' ;
435457
436- ``` javascript
437- import React from ' react' ;
438- import ReactDOM from ' react-dom/client' ;
439- import App from ' ./App' ;
440458import { FlowProvider } from ' @onflow/react-sdk' ;
441- import flowJSON from ' ./flow.json' ;
459+ import flowJSON from ' .. /flow.json' ;
442460
443- const root = ReactDOM .createRoot (document .getElementById (' root' ));
444- root .render (
445- < React .StrictMode >
461+ export default function FlowProviderWrapper({
462+ children ,
463+ }: {
464+ children: React .ReactNode ;
465+ }) {
466+ return (
446467 < FlowProvider
447468 config = {{
448469 accessNodeUrl : ' http://localhost:8888' , // Point to forked emulator REST endpoint
@@ -452,19 +473,43 @@ root.render(
452473 }}
453474 flowJson = {flowJSON }
454475 >
455- < App / >
476+ { children }
456477 < / FlowProvider >
457- < / React . StrictMode > ,
458- );
478+ );
479+ }
459480```
460481
461- Replace ` src/App.js ` with:
482+ Then update ` src/app/layout.tsx ` to use the wrapper:
483+
484+ ``` typescript
485+ import FlowProviderWrapper from ' @/components/FlowProviderWrapper' ;
486+
487+ export default function RootLayout({
488+ children ,
489+ }: {
490+ children: React .ReactNode ;
491+ }) {
492+ return (
493+ < html lang = " en" >
494+ <body >
495+ <FlowProviderWrapper >{children }< / FlowProviderWrapper >
496+ < / body >
497+ < / html >
498+ );
499+ }
500+ ```
501+
502+ ### Create a Demo Component
503+
504+ Create a simple demo that queries FlowToken supply from the forked mainnet. Update ` src/app/page.tsx ` :
505+
506+ ``` typescript
507+ ' use client' ;
462508
463- ``` javascript
464509import { useState } from ' react' ;
465510import { useFlowCurrentUser , useFlowQuery , Connect } from ' @onflow/react-sdk' ;
466511
467- function App () {
512+ export default function Home () {
468513 const { user } = useFlowCurrentUser ();
469514 const [shouldFetch, setShouldFetch] = useState (false );
470515
@@ -499,7 +544,7 @@ function App() {
499544 < button onClick = {() => setShouldFetch(true )} disabled = {isLoading }>
500545 {isLoading ? ' Loading...' : ' Get FlowToken Supply' }
501546 < / button >
502- {error && < p style= {{ color: ' red' }}> Error : {error .message }< / p> }
547+ {error && <p style = {{ color : ' red' }}> Error : {( error as Error ) .message }< / p > }
503548 {flowSupply && (
504549 <p style = {{ fontSize : ' 1.5rem' , color : ' green' }}>
505550 Total Supply : {Number (flowSupply ).toLocaleString ()} FLOW
@@ -519,8 +564,6 @@ function App() {
519564 < / div >
520565 );
521566}
522-
523- export default App ;
524567```
525568
526569### Start the dev wallet (optional)
@@ -535,13 +578,13 @@ This starts the dev wallet at `http://localhost:8701`.
535578
536579### Run your app
537580
538- Start the React app :
581+ Start the Next.js dev server :
539582
540583``` bash
541- npm start
584+ npm run dev
542585```
543586
544- Your browser will open to ` http://localhost:3000` . Click " Get FlowToken Supply" to see real mainnet data!
587+ Navigate to ` http://localhost:3000 ` . Click "Get FlowToken Supply" to see real mainnet data!
545588
546589** What's happening:**
547590
0 commit comments