You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/blockchain-development-tutorials/cadence/getting-started/building-a-frontend-app.md
+27-23Lines changed: 27 additions & 23 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -22,13 +22,13 @@ keywords:
22
22
23
23
# Building a Frontend App
24
24
25
-
Building on the `Counter` contract you deployed in [Cadence Environment Setup] and [Smart Contract Interaction], this tutorial shows you how to create a simple Next.js frontend that interacts with the `Counter` smart contract deployed on your local Flow emulator. Instead of using FCL directly, you'll leverage [**@onflow/react-sdk**] to simplify authentication, querying, transactions, and to display real-time transaction status updates using convenient React hooks.
25
+
This tutorial builds on the `Counter` contract you deployed in [Cadence Environment Setup] and [Smart Contract Interaction]. It shows you how to create a simple `Next.js` frontend that interacts with the `Counter` smart contract deployed on your local Flow emulator. Instead of using FCL directly, you'll leverage [**@onflow/react-sdk**] to simplify authentication, querying, transactions, and to display real-time transaction status updates using convenient React hooks.
26
26
27
27
## Objectives
28
28
29
-
After finishing this guide, you will be able to:
29
+
After you complete this tutorial, you will be able to:
30
30
31
-
- Wrap your Next.js app with a Flow provider using [**@onflow/react-sdk**].
31
+
- Wrap your `Next.js` app with a Flow provider using [**@onflow/react-sdk**].
32
32
- Read data from a Cadence smart contract (`Counter`) using kit's query hook.
33
33
- Send a transaction to update the smart contract's state using kit's mutation hook.
34
34
- Monitor a transaction's status in real time using kit's transaction hook.
@@ -40,7 +40,7 @@ After finishing this guide, you will be able to:
40
40
-[Flow CLI] installed.
41
41
- Node.js and npm installed.
42
42
43
-
## Setting Up the Next.js App
43
+
## Set Up the Next.js app
44
44
45
45
Follow these steps to set up your Next.js project and integrate [**@onflow/react-sdk**].
46
46
@@ -50,7 +50,7 @@ You can visit this [React-sdk Demo] to see how the hooks and components are used
50
50
51
51
:::
52
52
53
-
### Step 1: Create a New Next.js App
53
+
### Step 1: Create a new Next.js app
54
54
55
55
Run the following command in your project directory:
56
56
@@ -66,7 +66,7 @@ During setup, choose the following options:
66
66
67
67
This command creates a new Next.js project named `kit-app-quickstart` inside your current directory. We're generating the frontend in a subdirectory so we can next move it into our existing project structure from the previous steps (you can't create an app in a non-empty directory).
68
68
69
-
### Step 2: Move the Next.js App Up a Directory
69
+
### Step 2: Move the Next.js app Up a directory
70
70
71
71
Move the contents of the `kit-app-quickstart` directory into your project root. You can use the gui in your editor, or the console.
This library wraps FCL internally and exposes a set of hooks for authentication, querying, sending transactions, and tracking transaction status.
106
110
107
-
## Configuring the Local Flow Emulator and Dev Wallet
111
+
## Configure the local Flow Emulator and Dev Wallet
108
112
109
113
:::warning
110
114
111
-
You should already have the Flow emulator running from the local development step. If it's not running, you can start it again — but note that restarting the emulatorwill clear all blockchain state, including any contracts deployed in [Step 2: Local Development].
115
+
You should already have the Flow emulator running from the local development step. If it's not running, you can start it again — but when you restart the emulator, it will clear all blockchain state, which includes any contracts deployed in [Step 2: Local Development].
112
116
113
117
:::
114
118
@@ -132,9 +136,9 @@ flow dev-wallet
132
136
133
137
This will start the [Dev Wallet] on `http://localhost:8701`, which you'll use for authentication during development.
134
138
135
-
## Wrapping Your App with FlowProvider
139
+
## Wrap Your app with FlowProvider
136
140
137
-
[**@onflow/react-sdk**] provides a `FlowProvider` component that sets up the Flow Client Library configuration. In Next.js using the App Router, add or update your `src/app/layout.tsx` as follows:
141
+
[**@onflow/react-sdk**] provides a `FlowProvider` component that sets up the Flow Client Library configuration. In `Next.js` using the App Router, add or update your `src/app/layout.tsx` as follows:
138
142
139
143
```tsx
140
144
'use client';
@@ -170,11 +174,11 @@ This configuration initializes the kit with your local emulator settings and map
170
174
171
175
For more information on Discovery configurations, refer to the [Wallet Discovery Guide].
172
176
173
-
## Interacting With the Chain
177
+
## Interact With the chain
174
178
175
179
Now that we've set our provider, lets start interacting with the chain.
176
180
177
-
### Querying the Chain
181
+
### Query the chain
178
182
179
183
First, use the kit's [`useFlowQuery`] hook to read the current counter value from the blockchain.
180
184
@@ -208,7 +212,7 @@ This script fetches the counter value, formats it via the `NumberFormatter`, and
208
212
209
213
:::
210
214
211
-
### Sending a Transaction
215
+
### Send a transaction
212
216
213
217
Next, use the kit's [`useFlowMutate`] hook to send a transaction that increments the counter.
This sends a Cadence transaction to the blockchain using the `mutate` function. The transaction imports the `Counter` contract and calls its `increment` function. Authorization is handled automatically by the connected wallet during the `prepare` phase. Once submitted, the returned `txId` can be used to track the transaction's status in real time.
251
+
This sends a Cadence transaction to the blockchain using the `mutate` function. The transaction imports the `Counter` contract and calls its `increment` function. The connected wallet handles authorization automatically during the `prepare` phase. After it's submitted, you cna use the returned `txId` to track the transaction's status in real time.
248
252
249
-
### Subscribing to Transaction Status
253
+
### Subscribe to transaction status
250
254
251
255
Use the kit's [`useFlowTransactionStatus`] hook to monitor and display the transaction status in real time.
252
256
@@ -275,19 +279,19 @@ useEffect(() => {
275
279
-`2`: **Finalized** – The transaction has been included in a block, but not yet executed.
276
280
-`3`: **Executed** – The transaction code has run successfully, but the result has not yet been sealed.
277
281
-`4`: **Sealed** – The transaction is fully complete, included in a block, and now immutable onchain.
278
-
- We recommend calling`refetch()` when the status reaches **3 (Executed)** to update your UI more quickly after the transaction runs, rather than waiting for sealing.
282
+
- We recommend that you call`refetch()` when the status reaches **3 (Executed)** to update your UI more quickly after the transaction runs, rather than waiting for sealing.
279
283
- The `statusString` property gives a human-readable version of the current status you can display in the UI.
280
284
281
-
#### Why `Executed` is Recommended for UI Updates:
285
+
#### Why we recommend `Executed` for UI Updates:
282
286
283
287
Waiting for `Sealed` provides full onchain confirmation but can introduce a delay — especially in local or test environments. Since most transactions (like incrementing a counter) don't require strong finality guarantees, you can typically refetch data once the transaction reaches `Executed` for a faster, more responsive user experience.
284
288
285
289
However:
286
290
287
-
- If you're dealing with critical state changes (e.g., token transfers or contract deployments), prefer waiting for `Sealed`.
291
+
- If you're dealing with critical state changes (for example, token transfers or contract deployments), prefer waiting for `Sealed`.
288
292
- For non-critical UI updates, `Executed` is usually safe and significantly improves perceived performance.
289
293
290
-
### Integrating Authentication and Building the Complete UI
294
+
### Integrate authentication and build the complete UI
291
295
292
296
Finally, integrate the query, mutation, and transaction status hooks with authentication using `useFlowCurrentUser`. Combine all parts to build the complete page.
293
297
@@ -413,7 +417,7 @@ In this tutorial, we inlined Cadence code for simplicity. For real projects, we
413
417
414
418
:::
415
419
416
-
## Running the App
420
+
## Run the app
417
421
418
422
Start your development server:
419
423
@@ -442,9 +446,9 @@ Then visit [http://localhost:3000](http://localhost:3000) in your browser. You s
442
446
- Once logged in, your account address appears with options to **Log Out** and **Increment Count**.
443
447
- When you click **Increment Count**, the transaction is sent; its status updates are displayed in real time below the action buttons, and once the transaction is sealed, the updated count is automatically fetched.
444
448
445
-
## Wrapping Up
449
+
## Conclusion
446
450
447
-
By following these steps, you've built a simple Next.js dApp that interacts with a Flow smart contract using [**@onflow/react-sdk**]. In this guide you learned how to:
451
+
By following these steps, you've built a simple `Next.js` dApp that interacts with a Flow smart contract using [**@onflow/react-sdk**]. In this guide you learned how to:
448
452
449
453
- Wrap your application in a `FlowProvider` to configure blockchain connectivity.
450
454
- Use kit hooks such as `useFlowQuery`, `useFlowMutate`, `useFlowTransactionStatus`, and `useFlowCurrentUser` to manage authentication, query onchain data, submit transactions, and monitor their status.
0 commit comments