Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
React and React Native - Fifth Edition

You're reading from  React and React Native - Fifth Edition

Product type Book
Published in Apr 2024
Publisher Packt
ISBN-13 9781805127307
Pages 508 pages
Edition 5th Edition
Languages
Authors (2):
Mikhail Sakhniuk Mikhail Sakhniuk
Profile icon Mikhail Sakhniuk
Adam Boduch Adam Boduch
Profile icon Adam Boduch
View More author details

Table of Contents (33) Chapters

Preface 1. Part I: React
2. Why React? 3. Rendering with JSX 4. Understanding React Components and Hooks 5. Event Handling in the React Way 6. Crafting Reusable Components 7. Type-Checking and Validation with TypeScript 8. Handling Navigation with Routes 9. Code Splitting Using Lazy Components and Suspense 10. User Interface Framework Components 11. High-Performance State Updates 12. Fetching Data from a Server 13. State Management in React 14. Server-Side Rendering 15. Unit Testing in React 16. Part II: React Native
17. Why React Native? 18. React Native under the Hood 19. Kick-Starting React Native Projects 20. Building Responsive Layouts with Flexbox 21. Navigating Between Screens 22. Rendering Item Lists 23. Geolocation and Maps 24. Collecting User Input 25. Responding to User Gestures 26. Showing Progress 27. Displaying Modal Screens 28. Using Animations 29. Controlling Image Display 30. Going Offline 31. Other Books You May Enjoy
32. Index

Server-Side Rendering

As we discussed in Chapter 1, Why React?, the React library is remarkably flexible in terms of how our components can be transformed into various target formats. One such target format, as you might have guessed, is standard HTML markup, presented as a string and generated on the server. In this chapter, we will delve into how server-side rendering (SSR) works in React and the advantages it offers both users and developers. You will learn why this approach can be valuable for your application and how it enhances the overall user experience and performance.

The following topics are covered in this chapter:

  • Working on the server
  • Using Next.js
  • React Server Components

Technical requirements

You can find the code files of this chapter on GitHub at https://github.com/PacktPublishing/React-and-React-Native-5E/tree/main/Chapter13.

Working on the server

Web technologies have come a long way or, more precisely, have come full circle. It all started with static web pages prepared by a server. Servers were the foundation of all website and application logic, as they were entirely responsible for their functioning. Then, we tried to move away from SSR in favor of rendering pages in the browser, which led to a significant leap in the development of web pages as fully fledged applications, now comparable to desktop ones. As a result, browsers became the core of application logic, while servers merely provided data for applications.

Currently, the development cycle has brought us back to SSR and server components, but now we have a unified logic and code for both the server and the client. Why this happened and what conclusions and experiences we have gained with the evolution of technologies are what we will try to understand in this section, and we will simultaneously learn about the types of work our applications...

Using Next.js

After familiarizing ourselves with the theory of SSR, let’s see how we can implement all this in practice using the Next.js framework.

Next.js is a popular React-based framework specifically designed to simplify the process of SSR and static site generation. It offers powerful and flexible capabilities for creating high-performance web applications.

The features of Next.js:

  • An easy-to-use API that automates SSR and static generation: You just need to write code using the provided methods and functions, and the framework will automatically determine which pages should be rendered server-side and which can be rendered during the project build process.
  • File-based routing: Next.js uses a simple and intuitive routing system based on the folder and file structure in the project. This greatly simplifies the creation and management of routes in the application.
  • The ability to create comprehensive full-stack applications, thanks to API routes...

React Server Components

React Server Components represent a new paradigm for working with components in Next.js that eliminates isomorphic JavaScript. The code of such components runs only on a server and can be cached as a result. In this concept, you can directly read the server’s filesystem or access the database from the components.

In Next.js, React Server Components allow you to categorize components into two types: server-side and client-side. Server-side components are processed on a server and sent to the client as static HTML, reducing the load on the browser. Client-side components still have all the capabilities of browser JavaScript but with one requirement: you need to use the use client directive at the beginning of the file.

To use server-side components in Next.js, you will need to create a new project. For routing, you still use files, but now, the main folder for the project is the app folder, and route names are based solely on folder names. Inside...

Summary

In this chapter, we explored SSR in the context of React applications. We discussed approaches such as SSR, SSG, and ISR, learning the advantages and disadvantages of each approach.

Then, we learned how to apply these approaches in an application using Next.js and the Pages Router. Finally, we introduced a new technology called React Server Components and the updated Next.js architecture called the App Router.

In the next chapter, we will learn how to test our components and applications.

lock icon The rest of the chapter is locked
You have been reading a chapter from
React and React Native - Fifth Edition
Published in: Apr 2024 Publisher: Packt ISBN-13: 9781805127307
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime}