mcpField
R
react-19

react-19

github.com/prowler-cloud开发15.1k671 次安装今天更新
代码仓库

关于 react-19

面向开发者的 React 19 技能指南,说明组件、导入和数据处理规范,涵盖 Server Components、Actions、use() Hook 及将 ref 作为属性传递。

主要功能

  1. 01React Compiler 记忆化规范
  2. 02命名导入规范
  3. 03优先使用 Server Components
  4. 04使用 use() 读取 Promise
  5. 05Actions 与 useActionState
  6. 06将 ref 作为属性

适用场景

  1. 01判断何时使用客户端组件
  2. 02在组件中读取 Promise
  3. 03使用 Actions 处理表单提交
  4. 04向函数组件传递 ref

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/prowler-cloud/prowler --skill react-19

安装后在 Claude Code 中输入/react-19确认已加载。

详细介绍

不进行手动记忆化(必需)

// ✅ React Compiler handles optimization automatically
function Component({ items }) {
  const filtered = items.filter(x => x.active);
  const sorted = filtered.sort((a, b) => a.name.localeCompare(b.name));

  const handleClick = (id) => {
    console.log(id);
  };

  return <List items={sorted} onClick={handleClick} />;
}

// ❌ NEVER: Manual memoization
const filtered = useMemo(() => items.filter(x => x.active), [items]);
const handleClick = useCallback((id) => console.log(id), []);

导入(必需)

// ✅ ALWAYS: Named imports
import { useState, useEffect, useRef } from "react";

// ❌ NEVER
import React from "react";
import * as React from "react";

优先使用服务端组件

// ✅ Server Component (default) - no directive
export default async function Page() {
  const data = await fetchData();
  return <ClientComponent data={data} />;
}

// ✅ Client Component - only when needed
"use client";
export function Interactive() {
  const [state, setState] = useState(false);
  return <button onClick={() => setState(!state)}>Toggle</button>;
}

何时使用 "use client"

  • useState、useEffect、useRef、useContext
  • 事件处理程序(onClick、onChange)
  • 浏览器 API(window、localStorage)

use() Hook

import { use } from "react";

// Read promises (suspends until resolved)
function Comments({ promise }) {
  const comments = use(promise);
  return comments.map(c => <div key={c.id}>{c.text}</div>);
}

// Conditional context (not possible with useContext!)
function Theme({ showTheme }) {
  if (showTheme) {
    const theme = use(ThemeContext);
    return <div style={{ color: theme.primary }}>Themed</div>;
  }
  return <div>Plain</div>;
}

Action 与 useActionState

"use server";
async function submitForm(formData: FormData) {
  await saveToDatabase(formData);
  revalidatePath("/");
}

// With pending state
import { useActionState } from "react";

function Form() {
  const [state, action, isPending] = useActionState(submitForm, null);
  return (
    <form action={action}>
      <button disabled={isPending}>
        {isPending ? "Saving..." : "Save"}
      </button>
    </form>
  );
}

将 ref 用作属性(无需 forwardRef)

// ✅ React 19: ref is just a prop
function Input({ ref, ...props }) {
  return <input ref={ref} {...props} />;
}

// ❌ Old way (unnecessary now)
const Input = forwardRef((props, ref) => <input ref={ref} {...props} />);

评论

支持 Markdown