diff --git a/articles/_raw/nextjs-vs-react-differences.md b/articles/_raw/nextjs-vs-react-differences.md new file mode 100644 index 0000000..e1fa85d --- /dev/null +++ b/articles/_raw/nextjs-vs-react-differences.md @@ -0,0 +1,221 @@ +--- +title: Next.js vs React – Differences and How to Choose the Right One for Your Project +date: 2024-12-11T14:30:03.769Z +author: Okoro Emmanuel Nzube +authorURL: https://www.freecodecamp.org/news/author/Derekvibe/ +originalURL: https://www.freecodecamp.org/news/nextjs-vs-react-differences/ +posteditor: "" +proofreader: "" +--- + +As a developer, there are many tools, languages, frameworks and open-source packages you have to learn in order to make your job easier and straightforward (though the journey isn’t a straightforward one but you will get there). + + + +Some of these tools, languages, or frameworks are used daily by community members and can undergo fundamental changes in how they are implemented or written over time. + +In this article, we will explore two popular JavaScript technologies, Next.js and React.js, by comparing their key differences, examining their strengths, and offering insights to help developers choose the best option for their projects. + +## Table of Contents + +- [Table of Contents][1] + +- [Understanding React][2] + + - [Client-side Rendering][3] + + - [Use Cases for React in Web Development][4] + +- [Exploring Next.js][5] + + - [Server-side Rendering][6] + + - [Use Cases for Next.js in web development][7] + +- [Key Difference Between Next.js and React][8] + + - [Rendering Methods: client-side vs. server-side][9] + + - [Performance Considerations][10] + + - [SEO Implications][11] + + - [Scalability and Project Complexity][12] + +- [When to Use React or Next.js][13] + + - [When to use React][14] + + - [When to Use Next.js][15] + +- [Conclusion][16] + + +## Understanding React + +We often confuse ourselves as to whether React is a JavaScript framework or not, but here is the answer to that question. React is not a JavaScript framework, it is a JavaScript library. Now, these two terminologies are often interchanged or misused, but I will explain them shortly. + +A library is a collection of already-written code that can be reused or called upon when building your own project. + +**Example:** Imagine a library where you go to study. The books are already available on the shelves – you simply pick the one you need and start reading. Similarly, in programming, a library provides ready-made tools that you can use in your project without starting from scratch. + +On the other hand, a framework is like a ready-made structure that helps you build your project. It gives you a solid foundation to work with, so you don't have to start from scratch or write repetitive code. Instead, you focus on adding your own features and logic, while the framework takes care of running things at the right time and in the right way. + +**Example:** Think of a framework like a house under construction where the walls, foundation, and roof are already built. All you need to do is decide how to design the interior—like choosing the furniture, paint, and decorations. The framework handles the heavy lifting, like ensuring the house stands strong, while you focus on making it your own. Similarly, in programming, the framework provides the structure, and you add your custom logic to complete the project. + +With that out of the way, let’s move on. + +React is one of the most popular JavaScript libraries used by developers to build fast, interactive, and reliable user interfaces. It is a declarative library that helps developers create component-based web applications. Facebook developed this library in 2011 and it has been trending since then. + +Usually, when writing JavaScript code, we create a file with the extension `js`. For example: `App.js`, `script.js`, and so on. In React we create a file with the extension `jsx`. That is: `index.jsx`, `Home.jsx`, and so on. The `jsx` is a React extension that allows you to write a JavaScript code resembling HTML. The syntax, when executed, is passed through preprocessors/transpilers which transforms the HTML-looking code into a standard JavaScript code. + +At the heart of all React applications are components. Components are chunks of user interfaces (UI) which are built independently and can be reused in different parts of your project. Different components can be built separately and later brought together to form a complex user interface (UI). + +**Note:** Every React application has at least one component, commonly referred to as the root component. This component represents the entire application. Within the root component, there are often other components, known as child components, that help structure and manage different parts of the application. + +Here is a structural representation of root and child components. + +![structural representation of root and child components](https://cdn.hashnode.com/res/hashnode/image/upload/v1732758941272/17c6b471-b2a7-40ae-83f8-4e215a50c853.png) + +From the image above, you can clearly understand what components are all about. `App` is the root component, and inside the root component, we have the child components: `Navbar`, `Profile`, `Blog` and `Footer`. The child components can be reused on other pages of the project without having to rewrite the code again. + +### Client-side Rendering + +Client-side rendering (CSR) is a common technique, especially in libraries like React and frameworks like Vue.js, Angular, and so on. Here, the browser downloads and processes JavaScript files to dynamically render content directly on the user's device. With CSR, web pages are generated dynamically, and any updates or changes to the code are applied without requiring a full page reload. Only the specific parts that have changed are updated, ensuring a seamless and efficient user experience. + +Therefore, in CSR, the logic and structure of the web page is handled by the client (browser) and a fully rendered page is displayed. + +To help you understand CSR, [I have added an article here][17]. + +### **Use Cases for React in Web Development** + +Ever since React became a go-to choice for many developers, its flexibility has made it suitable for a wide range of use cases in web development. Here are a few cases: + +- **Single Page Applications (SPAs):** When we talk about SPAs, we don’t really mean that your web application has only one page, it can go on to having multiple pages. In SPAs, your web application files (HTML, CSS, JS) are generated once on your web page and when subsequent updates are made on the file, your page won’t have to fully reload. This approach helps ensure a faster transition, reduces load on the server and enhance overall user experience. + +- **Interactive User Interfaces:** React is suitable for building interactive user interfaces which, from time to time, undergo dynamic updates based on the actions of the users. Examples are online forms, dashboards, websites (E-commerce websites), and so on. + +- **Cross-Platform Applications:** Having React knowledge comes in handy when building mobile applications, simplifying the connection between web applications and mobile applications. Tools like React Native helps you achieve this process. + + +## **Exploring Next.js** + +Next.js is a popular React-based framework used in building web applications with the use of React components. Next.js provides additional structure, features, and optimization for your web application. + +Unlike React, Next.js supports server-side rendering (SSR), so requests are processed and generated from the server and then displayed on the browser (client). + +### **Server-side Rendering** + +Server-side rendering (SSR) is a technique in web development where a server generates the HTML for a web page on the server and sends it to the browser (client). In other words, the server handles the structures and logic of the page and displays a fully rendered page on the screen. + +In server-side rendering, a request is first sent to the server from the browser (client), then the server begins to process the request and when it's done processing the request, it executes the request by generating and displaying an HTML file with the content on the browser (client side). When a change is made or a new page is requested, a new request is sent again to the server and it is processed all over again – a fresh, fully rendered HTML file will be generated and displayed on the browser (client). + +For a better understanding of CSR and SSR, I have added a [YouTube video here][18]. + +### Use Cases for Next.js in web development + +- **Single Page Application (SPAs):** Next.js can be used in the creation of single page applications, similar to React. + +- **SEO-Friendly:** Next.js helps create SEO-friendly websites by rendering an HTML file on the server and delivering it to the browser. This improves search engine visibility, increasing the chances of your website appearing at the top of search results. + +- **Multi-User Platforms:** Due to Next.js ability to handle dynamic routing, API handling, and so on, it’s easy to create applications that serve various purposes. + + +## Key Difference Between Next.js and React + +### Rendering Methods: client-side vs. server-side + +When we talk about the rendering method in React, React relies mainly on rendering with the client-side rendering (CSR) method. Therefore both the logic and the structure of the web page will be handled by the browser (client). Though this method is commonly used, it has some downside effects like slower initial page load. + +Next.js, on the other hand, supports both SSR and CSR because it was built on top of React. Web pages are rendered on the server and both logic and structure of the page are all handled by the server. This enables a faster loading of the web page and also improves the SEO. + +### Performance Considerations + +In terms of performance considerations, Next.js is often preferred because it offers multiple rendering options, including server-side rendering (SSR), static site generation (SSG), Incremental Static Regeneration (ISR), and client-side rendering (CSR). In contrast, React primarily provides a single rendering approach: client-side rendering. + +### SEO Implications + +React is less SEO-friendly because search engines may struggle to index content that requires JavaScript execution to render. + +On the other hand, Next.js is more SEO-friendly than React because it renders content on the server, providing fully-rendered HTML to search engines for easier indexing. + +### Scalability and Project Complexity + +In terms of scalability and project complexity, Next.js is generally better than React. Next.js provides built-in features that enhance the scalability of your project. These include: + +- Server-side rendering (SSR) and static site generation (SSG) for better performance and SEO. + +- A built-in API routes feature for creating serverless functions seamlessly. + +- A file-based routing system that simplifies the organization of larger projects. + + +In contrast, with React, you are responsible for setting up and maintaining the structure for scalability. For larger projects, this often requires adding additional tools such as: + +- State management libraries (for example, Redux, Recoil, and so on). + +- Routing libraries (for example, React Router). + + +These tools are necessary to enhance React's scalability and address project complexity, but they also increase the overhead and effort needed to set up and manage the application. + +In summary, here is a tabular break down; + +

Factors

React

Next.js

Scalability

It is possible but to increase scalability, it requires additional tools and a custom setup

It is scalable and already has built-in tools that increase the scalability.

Performance

It provides only one rendering option which is client-side rendering (CSR)

It offers multiple rendering options, including SSR, SSG, ISR, and CSR.

SEO

It is less SEO-friendly because search engines may struggle to index content that requires JavaScript execution to render.

It is more SEO-friendly than React because it renders content on the server, providing fully-rendered HTML to search engines for easier indexing.

Use Case

Mostly used in smaller or unique projects

Mostly used in large-scale projects and enhances performance and SEO

+ +## When to Use React or Next.js + +Choosing the right tool for your project solely depends on the complexity of the solution of the project you are building. While React and Next.js are closely related, each has its strengths and optimal use cases. + +### When to use React + +Here are some cases when it is best to use React for your project: + +- When building highly interactive applications. + +- When your project requires manual handling of routing, state management or/and API integration. + +- When your project requires client-side rendering (CSR) + + +### When to Use Next.js + +Here are some cases where it’s best to use Next.js: + +- When your project requires a better SEO. + +- When your project requires server side rendering. + +- When your project requires you to build APIs along your frontend code. + +- When building content-driven websites like blogs or e-commerce sites. Due to its use of server-side rendering, it aids in improving the load times of contents on the page. + +- Next.js is best used when you want to optimize images in your project. + + +## Conclusion + +At this point I believe you have a clear understanding of React and Next.js, the concepts of server-side and client-side rendering, the use cases for both React and Next.js, and the key differences between them. + +Thank you for taking the time to read this. I hope you found it helpful. + +Happy coding. + +[1]: #heading-table-of-contents +[2]: #heading-understanding-react +[3]: #heading-client-side-rendering +[4]: #heading-use-cases-for-react-in-web-development +[5]: #heading-exploring-nextjs +[6]: #heading-server-side-rendering +[7]: #heading-use-cases-for-nextjs-in-web-development +[8]: #heading-key-difference-between-nextjs-and-react +[9]: #heading-rendering-methods-client-side-vs-server-side +[10]: #heading-performance-considerations +[11]: #heading-seo-implications +[12]: #heading-scalability-and-project-complexity +[13]: #heading-when-to-use-react-or-nextjs +[14]: #heading-when-to-use-react +[15]: #heading-when-to-use-nextjs +[16]: #heading-conclusion +[17]: https://www.freecodecamp.org/news/rendering-patterns/#heading-single-page-applications-spas-with-client-side-rendering-csr +[18]: https://youtu.be/-JXUaydU1J0?si=U3PrqicrIJoLYOM9 \ No newline at end of file diff --git a/articles/zh/nextjs-vs-react-differences.md b/articles/zh/nextjs-vs-react-differences.md new file mode 100644 index 0000000..9ddf2fd --- /dev/null +++ b/articles/zh/nextjs-vs-react-differences.md @@ -0,0 +1,219 @@ +```markdown +--- +title: Next.js 和 React 的区别及如何为您的项目选择合适的工具 +date: 2024-12-11T14:30:03.769Z +author: Okoro Emmanuel Nzube +authorURL: https://www.freecodecamp.org/news/author/Derekvibe/ +originalURL: https://www.freecodecamp.org/news/nextjs-vs-react-differences/ +posteditor: "" +proofreader: "" +--- + +作为一名开发者,有很多工具、语言、框架和开源包需要学习,以便让工作更轻松和直接(尽管这一路并不简单,但你会到达目的地)。 + + + +其中一些工具、语言或框架是社区成员每天都在使用的,随着时间的推移,它们在实施或编写方式上可能会发生根本性的变化。 + +在这篇文章中,我们将通过比较 Next.js 和 React.js 的关键差异来探索这两个流行的 JavaScript 技术,考察它们的优势,并为开发人员选择项目的最佳选项提供见解。 + +## 目录 + +- [目录][1] + +- [理解 React][2] + + - [客户端渲染][3] + + - [React 在 Web 开发中的使用案例][4] + +- [探索 Next.js][5] + + - [服务器端渲染][6] + + - [Next.js 在 Web 开发中的使用案例][7] + +- [Next.js 和 React 的关键区别][8] + + - [渲染方法:客户端 vs. 服务器端][9] + + - [性能考虑][10] + + - [SEO 影响][11] + + - [可扩展性和项目复杂性][12] + +- [何时使用 React 或 Next.js][13] + + - [何时使用 React][14] + + - [何时使用 Next.js][15] + +- [结论][16] + + +## 理解 React + +我们常常困惑于 React 是否是一个 JavaScript 框架,这里是答案。React 不是一个 JavaScript 框架,它是一个 JavaScript 库。现在,这两个术语经常被互换或误用,但我会在稍后解释。 + +库是一个已经编写好的代码集合,可以在构建自己的项目时重复使用或调用。 + +**例子:** 想象一个图书馆,你去那里学习。书已经在书架上可用——你只需挑选你需要的那本并开始阅读。类似地,在编程中,库提供了现成的工具,你可以在项目中使用,而无需从头开始。 + +另一方面,框架就像一个帮助你构建项目的现成的结构。它给你一个稳固的基础,让你无需从头开始或编写重复的代码。相反,你专注于添加自己的功能和逻辑,而框架负责让事情在正确的时间和方式下运行。 + +**例子:** 把框架想象成一个正在施工的房子,墙壁、地基和屋顶都已经建好。你只需要决定如何设计内部——比如选择家具、油漆和装饰。框架负责繁重的工作,比如确保房子稳固,而你则专注于让它成为你自己的。同样,在编程中,框架提供结构,而你添加自定义逻辑来完成项目。 + +说完这些,我们继续。 + +React 是开发人员用来构建快速、交互性强且可靠的用户界面的最流行的 JavaScript 库之一。它是一个声明式库,帮助开发人员创建基于组件的 Web 应用程序。Facebook 于 2011 年开发了这个库,并自那时起一直在流行。 + +通常,在编写 JavaScript 代码时,我们创建一个以 `js` 扩展名结尾的文件。例如:`App.js`、`script.js` 等。而在 React 中,我们创建一个以 `jsx` 扩展名结尾的文件。也就是:`index.jsx`、`Home.jsx` 等。`jsx` 是一个 React 扩展名,允许你写 JavaScript 代码,类似于 HTML。当语法被执行时,会通过预处理器/转译器,将看似 HTML 的代码转换为标准的 JavaScript 代码。 + +所有 React 应用程序的核心都是组件。组件是用户界面(UI)的独立块,可以在项目的不同部分重用。不同的组件可以独立构建,后来结合在一起形成一个复杂的用户界面(UI)。 + +**注意:** 每个 React 应用程序至少有一个组件,通常称为根组件。这个组件表示整个应用程序。在根组件中,通常还有其他组件,称为子组件,它们有助于结构化和管理应用程序的不同部分。 + +以下是根组件和子组件的结构示意图。 + +![根组件和子组件的结构示意图](https://cdn.hashnode.com/res/hashnode/image/upload/v1732758941272/17c6b471-b2a7-40ae-83f8-4e215a50c853.png) + +从上图中,你可以清楚地了解组件的全部含义。`App` 是根组件,而在根组件中,我们有子组件:`Navbar`、`Profile`、`Blog` 和 `Footer`。子组件可以在项目的其他页面中重用,而无需再次编写代码。 +``` + +客户端渲染(CSR)是一种常见技术,尤其是在像 React 这样的库和像 Vue.js、Angular 等框架中。在这种情况下,浏览器下载并处理 JavaScript 文件,以在用户设备上动态渲染内容。通过 CSR,网页是动态生成的,任何代码的更新或更改都会被应用而不需要全页重新加载。只有特定更改的部分会被更新,确保提供一个流畅且高效的用户体验。 + +因此,在 CSR 中,网页的逻辑和结构由客户端(浏览器)处理,并显示出一个完全渲染的页面。 + +为了帮助您理解 CSR,[我在这里添加了一篇文章][17]。 + +### **React 在 Web 开发中的用例** + +自从 React 成为许多开发者的首选以来,其灵活性使其适用于 Web 开发中的广泛用例。这里有几个例子: + +- **单页应用(SPAs):** 当我们谈论 SPAs 时,并不是说您的 Web 应用程序只有一个页面,它可以有多个页面。在 SPAs 中,您的 Web 应用程序文件(HTML、CSS、JS)会在网页上生成一次,当文件进行后续更新时,您的页面不需要完全重新加载。这种方法有助于确保更快的过渡,减少服务器负载并改善整体用户体验。 + +- **互动用户界面:** React 适合构建互动用户界面,这些界面会根据用户的操作不时地产生动态更新。例如在线表单、仪表盘、网站(电子商务网站)等。 + +- **跨平台应用程序:** 拥有 React 知识在构建移动应用程序时会非常方便,简化 Web 应用程序和移动应用程序之间的连接。像 React Native 这样的工具可以帮助您实现这个过程。 + +## **探索 Next.js** + +Next.js 是一个流行的基于 React 的框架,用于使用 React 组件构建 Web 应用程序。Next.js 为您的 Web 应用程序提供了额外的结构、功能和优化。 + +与 React 不同,Next.js 支持服务端渲染(SSR),请求从服务器处理并生成,然后在浏览器(客户端)上显示。 + +### **服务端渲染** + +服务端渲染(SSR)是一种 Web 开发技术,服务器在服务器上生成网页的 HTML,然后将其发送到浏览器(客户端)。换句话说,服务器处理页面的结构和逻辑,并在屏幕上展示一个完全渲染的页面。 + +在服务端渲染中,请求首先从浏览器(客户端)发送到服务器,然后服务器开始处理请求。当完成请求处理后,通过在浏览器(客户端)上生成和显示带有内容的 HTML 文件来执行请求。当进行更改或请求新页面时,再次发送新的请求到服务器并重新处理——一个新的完全渲染的 HTML 文件将被生成并显示在浏览器上(客户端)。 + +为了更好地理解 CSR 和 SSR,我在这里添加了一个 [YouTube 视频][18]。 + +### Next.js 在 Web 开发中的用例 + +- **单页应用(SPAs):** Next.js 可以用于创建单页应用程序,与 React 类似。 + +- **SEO 友好性:** Next.js 通过在服务器上渲染 HTML 文件并将其交付给浏览器,帮助创建 SEO 友好的网站。这提高了搜索引擎的可见性,增加了网站出现在搜索结果顶部的几率。 + +- **多用户平台:** 由于 Next.js具备处理动态路由、API处理等功能,因此易于创建用于各种目的的应用程序。 + +## Next.js 和 React 的关键区别 + +### 渲染方法:客户端 vs. 服务端 + +当我们谈到 React 的渲染方法时,React 主要依赖于客户端渲染(CSR)方法。因此网页的逻辑和结构将由浏览器(客户端)处理。尽管这种方法常用,但它有一些缺点,比如初始页面加载较慢。 + +而 Next.js 支持 SSR 和 CSR,因为它构建在 React 之上。网页在服务器上渲染,页面的逻辑和结构都由服务器处理。这可以加快网页加载速度并改善 SEO。 + +### 性能考虑 + +在性能考虑方面,Next.js 通常被首选,因为它提供多种渲染选项,包括服务端渲染(SSR)、静态站点生成(SSG)、增量静态再生(ISR)和客户端渲染(CSR)。相比之下,React 主要提供一种渲染方式:客户端渲染。 + +### SEO 影响 + +React 的 SEO 友好性较低,因为搜索引擎可能难以索引需要执行 JavaScript 才能渲染的内容。 + +另一方面,Next.js 比 React 更加 SEO 友好,因为它在服务器上渲染内容,为搜索引擎提供完全渲染的 HTML 以便于索引。 + +就可扩展性和项目复杂性而言,Next.js 通常比 React 更好。Next.js 提供了一些内置功能来增强项目的可扩展性。这些功能包括: + +- 服务器端渲染(SSR)和静态网站生成(SSG),以提升性能和搜索引擎优化(SEO)。 + +- 内置的 API 路由功能,可无缝创建无服务器函数。 + +- 基于文件的路由系统,简化了大型项目的组织。 + + +相比之下,使用 React 时,您需要负责为可扩展性设置和维护结构。对于大型项目,这通常需要添加额外的工具,例如: + +- 状态管理库(例如,Redux、Recoil 等)。 + +- 路由库(例如,React Router)。 + + +这些工具是提高 React 可扩展性和解决项目复杂性所必需的,但它们也增加了设置和管理应用程序所需的开销和工作量。 + +总之,以下是以表格方式拆分的总结; + +

因素

React

Next.js

可扩展性

是可能的,但要增加可扩展性需要额外的工具和自定义设置

具有可扩展性且已有内置工具来提高可扩展性。

性能

仅提供一种渲染选项,即客户端渲染(CSR)

提供多种渲染选项,包括 SSR、SSG、ISR 和 CSR。

SEO

SEO 友好程度较低,因为搜索引擎可能难以索引需要 JavaScript 执行才能渲染的内容。

比 React 更加 SEO 友好,因为它在服务器上渲染内容,向搜索引擎提供完全渲染的 HTML 以便于索引。

使用场景

主要用于较小或独特的项目

主要用于大规模项目,提升性能和 SEO

+ +## 何时使用 React 或 Next.js + +为你的项目选择合适的工具完全取决于你正在构建项目的解决方案复杂度。尽管 React 和 Next.js 密切相关,但每个都有其优势和最佳使用场景。 + +### 何时使用 React + +以下是一些适合使用 React 的项目场景: + +- 构建高度互动的应用程序时。 + +- 项目需要手动处理路由、状态管理或/及 API 集成时。 + +- 项目需要客户端渲染(CSR)时。 + + +### 何时使用 Next.js + +以下是一些适合使用 Next.js 的项目场景: + +- 项目需要更好的 SEO 时。 + +- 项目需要服务器端渲染时。 + +- 项目需要你在前端代码中构建 API 时。 + +- 构建内容驱动的网站,如博客或电子商务网站时。由于使用服务器端渲染,它有助于改善页面内容的加载时间。 + +- 当你想在项目中优化图片时,Next.js 是最佳选择。 + + +## 结论 + +此时,我相信你已经清楚了解 React 和 Next.js、服务器端和客户端渲染的概念、两者的使用场景以及它们之间的主要区别。 + +谢谢你的阅读,希望对你有所帮助。 + +编码愉快。 + +[1]: #heading-table-of-contents +[2]: #heading-understanding-react +[3]: #heading-client-side-rendering +[4]: #heading-use-cases-for-react-in-web-development +[5]: #heading-exploring-nextjs +[6]: #heading-server-side-rendering +[7]: #heading-use-cases-for-nextjs-in-web-development +[8]: #heading-key-difference-between-nextjs-and-react +[9]: #heading-rendering-methods-client-side-vs-server-side +[10]: #heading-performance-considerations +[11]: #heading-seo-implications +[12]: #heading-scalability-and-project-complexity +[13]: #heading-when-to-use-react-or-nextjs +[14]: #heading-when-to-use-react +[15]: #heading-when-to-use-nextjs +[16]: #heading-conclusion +[17]: https://www.freecodecamp.org/news/rendering-patterns/#heading-single-page-applications-spas-with-client-side-rendering-csr +[18]: https://youtu.be/-JXUaydU1J0?si=U3PrqicrIJoLYOM9 + + \ No newline at end of file