」工欲善其事,必先利其器。「—孔子《論語.錄靈公》
首頁 > 程式設計 > React - 伺服器操作

React - 伺服器操作

發佈於2024-08-16
瀏覽:769

React form Actions.

React has introduced new form Actions and related hooks to enhance native forms and streamline client-server communication. These features enable developers to handle form submissions more effectively, improving both user experience and code maintainability. For an in-depth exploration of React form Actions, you can refer to my detailed post on my post about React Form Actions.

Server Actions

With React 18, the Server Components feature was introduced. Server components are not Server-Side Rendering (SSR), Server Components are executed exclusively on the server during both runtime and build time. These components can access server-side resources, such as databases and the file system, but they are not capable of performing client-side actions like event listeners or hooks.

Prerequisites

To demonstrate the capabilities of Server Components and Server Actions, we'll use Next.js and Prisma.

Next.js is a React framework for building full-stack web applications. You use React Components to build user interfaces, and Next.js for additional features and optimizations. Under the hood, Next.js also abstracts and automatically configures tooling needed for React, like bundling, compiling, and more. This allows you to focus on building your application instead of spending time with configuration. learn more

Prisma is an ORM that simplifies database access and operations, allowing you to query and manipulate data without writing SQL. Learn more

Initial Setup
Start by creating a new Next.js application:
yarn create next-app server-example

Your initial folder structure will look like this:

React - Server Actions

Upgrade to the Canary Release to access React 19 features, including Server Actions:

yarn add next@rc react@rc react-dom@rc

install Prisma

yarn add prisma

Prisma Configuration
Create a Prisma schema file at src/lib/prisma/schema.prisma:

generator client {
  provider = "prisma-client-js"
}

datasource db {
  provider = "sqlite"
  url      = "file:./dev.db"
}

model User {
  id    Int     @id @default(autoincrement())
  email String  @unique
  name  String?
  age Int
}

For demonstration purposes, we are using SQLite. For production, you should use a more robust database.

Next, add a Prisma client file at src/lib/prisma/prisma.ts

// ts-ignore 7017 is used to ignore the error that the global object is not
// defined in the global scope. This is because the global object is only
// defined in the global scope in Node.js and not in the browser.

import { PrismaClient } from '@prisma/client'

// PrismaClient is attached to the `global` object in development to prevent
// exhausting your database connection limit.
//
// Learn more:
// https://pris.ly/d/help/next-js-best-practices

const globalForPrisma = global as unknown as { prisma: PrismaClient }

export const prisma = globalForPrisma.prisma || new PrismaClient()

if (process.env.NODE_ENV !== 'production') globalForPrisma.prisma = prisma

export default prisma

Configure Prisma in package.json:

{
  //other settings
  "prisma": {
    "schema": "src/lib/prisma/schema.prisma",
    "seed": "ts-node src/lib/prisma/seed.ts"
  }
}

And update TypeScript settings in tsconfig.json:

{
  //Other settings here...

  "ts-node": {
    // these options are overrides used only by ts-node
    // same as the --compilerOptions flag and the 
    // TS_NODE_COMPILER_OPTIONS environment variable
    "compilerOptions": {
      "module": "commonjs"
    }
  }
}

Install ts-node globally:

yarn global add ts-node

Seeding Initial Data
Add a seed file at src/lib/prisma/seed.ts to populate initial data:

import { PrismaClient } from "@prisma/client";
const prisma = new PrismaClient();
async function main() {
  await prisma.user.create({
    email: "[email protected]",
    name: "Anto",
    age: 35,
  });
  await prisma.user.create({
    email: "[email protected]",
    name: "Vinish",
    age: 32,
  });
}
main()
  .then(async () => {
    await prisma.$disconnect();
  })
  .catch(async (e) => {
    console.error(e);
    await prisma.$disconnect();
    process.exit(1);
  });

Install Prisma client

yarn add @prisma/client

Run the migration command:

yarn prisma migrate dev --name init

If the seed data is not reflected, add it manually:

yarn prisma db seed

Great! Since the installations are ready, you can create an actions file that performs database operations.

Creating Server Actions
Server Actions are a powerful feature that enables seamless client-server intercommunication. Let's create a file for database operations at src/actions/user.ts:

"use server";
import prisma from '@/lib/prisma/prisma'
import { revalidatePath } from "next/cache";

// export type for user
export type User = {
  id: number;
  name: string | null;
  email: string;
  age: number;
};


export async function createUser(user: any) {
  const resp = await prisma.user.create({ data: user });
  console.log("server Response");
  revalidatePath("/");
  return resp;
}

export async function getUsers() {
  return await prisma.user.findMany();
}

export async function deleteUser(id: number) {
  await prisma.user.delete({
    where: {
      id: id,
    },
  });
  revalidatePath("/");
}

Implementing Server Components

Let's create a React server component to read and render data from the database. Create src/app/serverexample/page.tsx:

import UserList from "./Users";
import "./App.css"

export default async function ServerPage() {
  return (
    
); }

Add some styling in src/app/serverexample/App.css

.App {
  text-align: center;
}

.App-logo {
  height: 40vmin;
  pointer-events: none;
}

.App-header {
  background-color: #282c34;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  font-size: calc(10px   2vmin);
  color: white;
}

input {
  color: #000;
}

.App-link {
  color: #61dafb;
}

Create components to fetch and render the user list:
src/app/serverexample/UserList.tsx

import { getUsers } from "@/actions/user";
import { UserDetail } from "./UserDetail";

export default async function UserList() {
  //Api call to fetch User details
  const users = await getUsers();

  return (
    
{users.length ? ( users.map((user) => ) ) : (
No User found
)}
); }

src/app/serverexample/UserDetail.tsx

export function UserDetail({ user }) {
  return (
    
avatar
{user.name}
{user.email}
); }

Run the development server:

yarn dev

Navigate to http://localhost:3000/serverexample to see the rendered user list:
React - Server Actions

By default, components in Next.js are server components unless you specify the "use client" directive. Notice two important points:

  1. Async Component Definition: Server components can be asynchronous as they do not re-render and are only generated once.
  2. Data Fetching: The line const users = await getUsers(); fetches data from the server and renders it at runtime.

Exploring Server Actions

Server Actions enable seamless client-server intercommunication. Let's add a form to create new users.

Create a new file at src/app/serverexample/AddUser.tsx:

"use client";

import "./app.css";
import { useActionState } from "react";
import { createUser } from "../../actions/user";

const initialState = {
  error: undefined,
};

export default function AddUser() {
  const submitHandler = async (_previousState: object, formData: FormData) => {
    try {
      // This is the Server Action method that transfers the control 
      // Back to the server to do DB operations and get back the result.
      const response = await createUser({
        name: formData.get("name") as string,
        email: formData.get("email") as string,
        age: parseInt(formData.get("age") as string),
      });
      return { response };
    } catch (error) {
      return { error };
    }
  };
  const [state, submitAction, isPending] = useActionState(
    submitHandler,
    initialState
  );

  return (
    

Add new User

{" "}
Name:{" "}
Email:{" "}
Age:{" "}
{(state?.error as string) &&

{state.error as string}

}
); }

Update src/app/serverexample/page.tsx to include the AddUser component:

import UserList from "./UserList";
// Import new line
import AddUser from "./AddUser";
import "./App.css"

export default async function ServerPage() {
  return (
    
{/* insert Add User here */}
); }

Running the application will now allow you to add new users via the form, with server-side processing handled seamlessly.
React - Server Actions

The AddUser Component and Seamless Client-Server Interaction

The AddUser component is at the heart of this example, showcasing how React Server Actions can revolutionize the way we handle client-server interactions. This component renders a form for adding new users and leverages the useActionState hook to create a smooth and seamless bridge between the client-side interface and server-side operations.

How It Works

  1. Form Rendering and Data Handling:
  • The AddUser component provides a form where users can input their name, email, and age.
  • Upon form submission, the data is captured and prepared to be sent to the server.
  1. useActionState Hook:
  • The useActionState hook is a crucial part of this setup. It simplifies the complexity of managing client-side state and server-side actions by abstracting them into a unified interface.
  • This hook accepts an asynchronous handler function, which processes the form data and then calls a Server Action method.
  • The brilliance of this approach lies in its abstraction: it feels as though you’re invoking a regular function within the same file, even though it actually triggers a server-side operation.
  1. Server Action Method:
  • The createUser function, defined as a Server Action, executes on the server side. It takes the user data from the form, performs the necessary database operations via Prisma, and returns the result.
  • This server-side method is crucial for maintaining a clean separation between the client and server, while still enabling them to communicate effectively.
  1. Seamless Integration:

From the perspective of a developer working on the client side, it appears as if the form submission is handled locally. However, the heavy lifting such as database manipulation occurs on the server.
The useActionState hook encapsulates this process, managing the state transitions and handling errors, while maintaining an intuitive API for developers.

Server Actions Without Forms

So that's with forms, now lets test an example without forms.
update src/app/serverexample/UserDetail.tsx

"use client";
import { deleteUser } from "@/actions/user";
import { useTransition } from "react";

export function UserDetail({ user }) {
  const [pending, startTransition] = useTransition();

  const handleDelete = () => {
    startTransition(() => {
      deleteUser(user.id);
    });
  };

  return (
    
{pending ? (

Deleting...

) : ( avatar
{user.name}
{user.email}
> )}
); }

Key Points:

  • Server Action: deleteUser(user.id) is a server action that removes the user from the database. This operation is triggered without any form submission.
  • useTransition: This hook allows you to manage the asynchronous state of the deletion process, showing a "Deleting..." message while the operation is in progress.
  • User Interface: The component maintains a clean UI, dynamically updating based on the action status.

Now, you can seamlessly delete a user within the application:
React - Server Actions

Conclusion

This approach is transformative because it abstracts away the complexities of client-server communication. Traditionally, such interactions would require handling API endpoints, managing asynchronous requests, and carefully coordinating client-side state with server responses. With React Server Actions and the useActionState hook, this complexity is reduced, allowing developers to focus more on building features rather than worrying about the underlying infrastructure.

By using this pattern, you gain:

  • Cleaner Code: The client-side code remains simple and focused, without the need for explicit API calls.
  • Improved Developer Experience: Server-side operations are seamlessly integrated, reducing cognitive load and potential for errors.
  • Enhanced Performance: Server Actions are optimized for performance, reducing unnecessary client-server round trips and ensuring that server-side resources are used efficiently.

You can find the full code in the repository

版本聲明 本文轉載於:https://dev.to/vinishanto/react-19-server-actions-234i?1如有侵犯,請聯絡[email protected]刪除
最新教學 更多>
  • 極簡生活的藝術
    極簡生活的藝術
    什麼是極簡生活? 極簡生活是一種有意減少擁有的財產數量和生活中雜亂的生活方式。這不僅是為了整理您的空間,也是為了簡化您的生活,專注於真正重要的事情,並減少干擾。 為什麼採用極簡主義? 頭腦清晰:擁有的東西越少,需要擔心的事情就越少,頭腦就越清晰。 財務自由:透過...
    程式設計 發佈於2024-11-06
  • Java 混淆之謎
    Java 混淆之謎
    Come play with our Java Obfuscator & try to deobfuscate this output. The price is the free activation code! Obfuscated Java code Your goal...
    程式設計 發佈於2024-11-06
  • 如何在沒有圖像的 Outlook 電子郵件中建立圓角?
    如何在沒有圖像的 Outlook 電子郵件中建立圓角?
    在沒有圖像的 Outlook 中設定圓角樣式使用 CSS 在電子郵件用戶端中建立圓角可以非常簡單。但是,使用 CSS border-radius 屬性的傳統方法在 Microsoft Outlook 中不起作用。在設計具有圓角元素的電子郵件時,此限制提出了挑戰。 不用擔心,有一個解決方案可以讓您在 ...
    程式設計 發佈於2024-11-06
  • 如何在Python中高效比較字典中相等的鍵值對?
    如何在Python中高效比較字典中相等的鍵值對?
    比較字典是否相等的鍵值對在Python中,比較字典以檢查鍵值對是否相等是一項常見任務。一種方法是迭代字典並使用 zip 和 iteritems 方法比較每一對字典。然而,還有一些替代方法可以提供更好的程式碼優雅性。 其中一種方法是使用字典理解來建立僅包含共享鍵值對的新字典。代碼如下:shared_i...
    程式設計 發佈於2024-11-06
  • 如何在 PHP 中使用數組函數向左旋轉數組元素?
    如何在 PHP 中使用數組函數向左旋轉數組元素?
    在PHP 中向左旋轉數組元素在PHP 中旋轉數組,將第一個元素移動到最後一個元素並重新索引數組,可以使用PHP 的array_push() 和array_shift() 函數組合來實現。 PHP 函數:PHP 沒有專門用於旋轉的內建函數數組。但是,以下程式碼片段示範如何模擬所需的旋轉行為:$numb...
    程式設計 發佈於2024-11-06
  • 如何解決Java存取檔案時出現「系統找不到指定的路徑」錯誤?
    如何解決Java存取檔案時出現「系統找不到指定的路徑」錯誤?
    解決Java 中遇到「系統找不到指定的路徑」時的檔案路徑問題在Java 專案中,嘗試存取文字時遇到錯誤來自指定相對路徑的檔案。此錯誤是由於 java.io.File 類別無法定位指定路徑而產生的。 要解決此問題,建議從類別路徑中檢索文件,而不是依賴文件系統。透過這樣做,您可以消除相對路徑的需要,並確...
    程式設計 發佈於2024-11-06
  • Laravel 中的 defer() 函數如何運作?
    Laravel 中的 defer() 函數如何運作?
    Taylor Otwell 最近宣布了 Laravel 中的新函數 defer()。這只是對 defer() 函數如何運作以及使用它可能遇到的問題進行非常基本的概述。 找出問題 還記得您曾經需要從 API 獲取某些內容,然後在幕後執行一些用戶不關心但仍在等待的操作的路由嗎?是的,我們都至少經歷過一...
    程式設計 發佈於2024-11-06
  • 在 Python Notebook 中探索使用 PySpark、Pandas、DuckDB、Polars 和 DataFusion 的資料操作
    在 Python Notebook 中探索使用 PySpark、Pandas、DuckDB、Polars 和 DataFusion 的資料操作
    Apache Iceberg Crash Course: What is a Data Lakehouse and a Table Format? Free Copy of Apache Iceberg the Definitive Guide Free Apache Iceberg Crash ...
    程式設計 發佈於2024-11-06
  • Vue + Tailwind 和動態類
    Vue + Tailwind 和動態類
    我最近在做的一個專案使用了Vite、Vue和Tailwind。 使用自訂顏色一段時間後,我遇到了一些困惑。 在模板中添加和使用自訂顏色不是問題 - 使用 Tailwind 文件使該過程非常清晰 // tailwind.config.js module.exports = { theme:...
    程式設計 發佈於2024-11-06
  • 端對端(E 測試:綜合指南
    端對端(E 測試:綜合指南
    端到端测试简介 端到端(E2E)测试是软件开发生命周期的重要组成部分,确保整个应用程序流程从开始到结束都按预期运行。与专注于单个组件或几个模块之间交互的单元或集成测试不同,端到端测试从用户的角度验证整个系统。这种方法有助于识别应用程序不同部分交互时可能出现的任何问题,确保无缝且无错误的用户体验。 ...
    程式設計 發佈於2024-11-06
  • 可以在 Go 結構標籤中使用變數嗎?
    可以在 Go 結構標籤中使用變數嗎?
    在Go 結構體標籤中嵌入變數Go 的結構體標籤通常用於註釋和元數據,通常涉及簡單的字符串文字。但是,使用者可能會遇到在這些標籤中需要動態或計算值的情況。 考慮以下結構,其中帶有為 JSON 封送註解的「類型」欄位:type Shape struct { Type string `json:&...
    程式設計 發佈於2024-11-06
  • 如何增強 Visual Studio 的建置詳細程度以實現深入洞察?
    如何增強 Visual Studio 的建置詳細程度以實現深入洞察?
    熟悉 Visual Studio 的建造詳細程度需要全面了解 Visual Studio 建置過程背後的複雜細節?別再猶豫了! 雖然使用 vcbuild 不會產生所需的詳細輸出,但 Visual Studio 的設定中隱藏著一個解決方案。採取以下簡單步驟即可解鎖大量資訊:導覽至 Visual Stu...
    程式設計 發佈於2024-11-06
  • 開發者日記# 誰寫的?
    開發者日記# 誰寫的?
    有個想法困擾著我。也許,我們無法識別它,但日復一日,我們周圍越來越多的人工智慧生成的內容。 LinkedIn 或其他平台上的有趣圖片、影片或貼文。我對帖子的媒體內容沒有疑問(很容易識別它何時生成、從庫存中獲取或創建),但我對帖子的內容表示懷疑。幾乎每次我讀一篇文章時,我都會想這是誰寫的?是作者分享了...
    程式設計 發佈於2024-11-06
  • 哪一種方法計算資料庫行數較快:PDO::rowCount 或 COUNT(*)?
    哪一種方法計算資料庫行數較快:PDO::rowCount 或 COUNT(*)?
    PDO::rowCount 與COUNT(*) 效能在資料庫查詢中計算行數時,選擇使用PDO:: rowCount 和COUNT(*) 會顯著影響效能。 PDO::rowCountPDO::rowCount 傳回受最後一個 SQL 語句影響的行數。但是,對於 SELECT 語句,某些資料庫可能會傳回...
    程式設計 發佈於2024-11-06
  • PART# 使用 HTTP 進行大型資料集的高效能檔案傳輸系統
    PART# 使用 HTTP 進行大型資料集的高效能檔案傳輸系統
    让我们分解提供的HTML、PHP、JavaScript和CSS代码对于分块文件上传仪表板部分。 HTML 代码: 结构概述: Bootstrap for Layout:代码使用 Bootstrap 4.5.2 创建一个包含两个主要部分的响应式布局: 分块上传部分:用于...
    程式設計 發佈於2024-11-06

免責聲明: 提供的所有資源部分來自互聯網,如果有侵犯您的版權或其他權益,請說明詳細緣由並提供版權或權益證明然後發到郵箱:[email protected] 我們會在第一時間內為您處理。

Copyright© 2022 湘ICP备2022001581号-3