启动一个新的 React 项目

如果你想完全用 React 构建一个新的应用或网站,我们建议选择社区中流行的、由 React 驱动的框架。

You can use React without a framework, however we’ve found that most apps and sites eventually build solutions to common problems such as code-splitting, routing, data fetching, and generating HTML. These problems are common to all UI libraries, not just React.

By starting with a framework, you can get started with React quickly, and avoid essentially building your own framework later.

深入探讨

Can I use React without a framework?

You can definitely use React without a framework—that’s how you’d use React for a part of your page. However, if you’re building a new app or a site fully with React, we recommend using a framework.

Here’s why.

Even if you don’t need routing or data fetching at first, you’ll likely want to add some libraries for them. As your JavaScript bundle grows with every new feature, you might have to figure out how to split code for every route individually. As your data fetching needs get more complex, you are likely to encounter server-client network waterfalls that make your app feel very slow. As your audience includes more users with poor network conditions and low-end devices, you might need to generate HTML from your components to display content early—either on the server, or during the build time. Changing your setup to run some of your code on the server or during the build can be very tricky.

These problems are not React-specific. This is why Svelte has SvelteKit, Vue has Nuxt, and so on. To solve these problems on your own, you’ll need to integrate your bundler with your router and with your data fetching library. It’s not hard to get an initial setup working, but there are a lot of subtleties involved in making an app that loads quickly even as it grows over time. You’ll want to send down the minimal amount of app code but do so in a single client–server roundtrip, in parallel with any data required for the page. You’ll likely want the page to be interactive before your JavaScript code even runs, to support progressive enhancement. You may want to generate a folder of fully static HTML files for your marketing pages that can be hosted anywhere and still work with JavaScript disabled. Building these capabilities yourself takes real work.

React frameworks on this page solve problems like these by default, with no extra work from your side. They let you start very lean and then scale your app with your needs. Each React framework has a community, so finding answers to questions and upgrading tooling is easier. Frameworks also give structure to your code, helping you and others retain context and skills between different projects. Conversely, with a custom setup it’s easier to get stuck on unsupported dependency versions, and you’ll essentially end up creating your own framework—albeit one with no community or upgrade path (and if it’s anything like the ones we’ve made in the past, more haphazardly designed).

If your app has unusual constraints not served well by these frameworks, or you prefer to solve these problems yourself, you can roll your own custom setup with React. Grab react and react-dom from npm, set up your custom build process with a bundler like Vite or Parcel, and add other tools as you need them for routing, static generation or server-side rendering, and more.

生产级的 React 框架

These frameworks support all the features you need to deploy and scale your app in production and are working towards supporting our full-stack architecture vision. All of the frameworks we recommend are open source with active communities for support, and can be deployed to your own server or a hosting provider. If you’re a framework author interested in being included on this list, please let us know.

Next.js

Next.js’ Pages Router is a full-stack React framework. It’s versatile and lets you create React apps of any size—from a mostly static blog to a complex dynamic application. To create a new Next.js project, run in your terminal:

Terminal
npx create-next-app@latest

如果你是 Next.js 的新手,请查看 Next.js 课程

Next.js 由 Vercel 维护。你可以 将 Next.js 应用 部署到 Node.js 或 serverless 上,也可以部署到你自己的服务器上。完全静态的 Next.js 应用 可以部署在任何支持静态服务的地方。

Remix

Remix 是一个具有嵌套路由的全栈式 React 框架。它可以把你的应用分成嵌套部分,该嵌套部分可以并行加载数据并响应用户操作进行刷新。要创建一个新的 Remix 项目,请运行:

Terminal
npx create-remix

如果你是 Remix 的新手,请查看 Remix 的 博客教程(短)和 应用教程(长)。

Remix 由 Shopify 维护。当你创建一个 Remix 项目时,你需要 选择你的部署目标。你可以通过使用或编写 适配器 将 Remix 应用部署到 Node.js 或 serverless 上进行托管。

Gatsby

Gatsby 是一个快速的支持 CMS 的网站的 React 框架。其丰富的插件生态系统和 GraphQL 数据层简化了将内容、API 和服务整合到一个网站的过程。要创建一个新的 Gatsby 项目,请运行:

Terminal
npx create-gatsby

如果你是 Gatsby 的新手,请查看 Gatsby 教程

Gatsby 由 Netlify 维护。你可以 部署一个完全静态的 Gatsby 网站 到任何一个支持静态服务的地方。如果你选择使用服务器上的功能,请确保你的主机供应商支持 Gatsby 的功能。

Expo(用于原生应用)

Expo 是一个 React 框架,可以让你创建具有真正原生 UI 的应用,包括 Android、iOS,以及 Web 应用。它为 React Native 提供了 SDK,使原生部分更容易使用。要创建一个新的 Expo 项目,请运行:

Terminal
npx create-expo-app

如果你是 Expo 的新手,请查看 Expo 教程

Expo 是由 Expo 这家公司 维护的。用 Expo 构建应用是免费的,而且你可以不受限制地将它们提交到谷歌和苹果的应用商店。此外,Expo 还提供选择性的付费云服务。

Bleeding-edge React frameworks

在我们探索如何继续改进 React 的过程中,我们意识到将 React 与框架(特别是路由、bundle 和服务端技术)更紧密地结合起来是我们帮助 React 用户构建更好的应用的最大机会。Next.js 团队已经同意与我们合作,研究、开发、集成和测试与框架无关的 React 前沿功能,如 React 服务器组件

这些功能每天都在接近生产就绪,而且我们一直在与其他 bundler 和框架的开发者讨论整合它们。我们希望在一两年内,这个页面上列出的所有框架都能完全支持这些功能。(如果你是一个框架的作者,有兴趣与我们合作来实验这些功能,请告诉我们!)

Next.js (App Router)

Next.js 的 App Router 是对 Next.js API 的重新设计,旨在实现 React 团队的全栈架构愿景。它让你在异步组件中获取数据,这些组件甚至能在服务端构建过程中运行。

Next.js 由 Vercel 维护。你可以将 Next.js 应用 部署到 Node.js 或 serverless 主机上,或部署到你自己的服务器上。Next.js 还支持 静态导出,不需要服务器。

深入探讨

哪些功能构成了 React 团队的全栈架构愿景?

Next.js 的 App Router bundler 完全实现了官方的 React 服务器组件规范。这让你可以在一棵 React 树上同时使用 构建时纯服务端交互组件

例如,你可以把一个纯服务端的 React 组件写成一个 async 函数,从数据库或文件中读取。然后你可以把数据从它那里传递给你的交互组件:

// 这个组件只在服务端运行(或在构建期间)。
async function Talks({ confId }) {
// 1. 你在服务端,所以你可以和你的数据层对话。不需要 API 端点。
const talks = await db.Talks.findAll({ confId });

// 2. 添加任意数量的渲染逻辑。它不会使你的 JavaScript bundle 变大。
const videos = talks.map(talk => talk.video);

// 3. 将数据向下传递给将在浏览器中运行的组件。
return <SearchableVideoList videos={videos} />;
}

Next.js 的 App Router 还集成了 用 Suspense 获取数据的能力。这让你可以直接在 React 树中为用户界面的不同部分指定一个加载状态(像一个骨架占位符):

<Suspense fallback={<TalksLoading />}>
<Talks confId={conf.id} />
</Suspense>

服务器组件和 Suspense 是 React 的功能,不是由 Next.js 提供的。然而,在框架层面上采用它们需要投入大量的实现工作。目前,Next.js App Router 是最完整的实现。React 团队正在与 bundler 的开发人员合作,使这些功能在下一代框架中更容易实现。