Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
React Key Concepts
React Key Concepts

React Key Concepts: An in-depth guide to React's core features , Second Edition

Arrow left icon
Profile Icon Maximilian Schwarzmüller
Arrow right icon
€20.98 €29.99
eBook Jan 2025 544 pages 2nd Edition
eBook
€20.98 €29.99
Paperback
€37.99
Subscription
Free Trial
Renews at €18.99p/m
Arrow left icon
Profile Icon Maximilian Schwarzmüller
Arrow right icon
€20.98 €29.99
eBook Jan 2025 544 pages 2nd Edition
eBook
€20.98 €29.99
Paperback
€37.99
Subscription
Free Trial
Renews at €18.99p/m
eBook
€20.98 €29.99
Paperback
€37.99
Subscription
Free Trial
Renews at €18.99p/m

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Product feature icon AI Assistant (beta) to help accelerate your learning
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Table of content icon View table of contents Preview book icon Preview Book

React Key Concepts

Summary and Key Takeaways

  • React is a library, though it's actually a combination of two main packages: react and react-dom.
  • Though it is possible to build non-trivial user interfaces without React, simply using vanilla JavaScript to do so can be cumbersome, error-prone, and hard to maintain.
  • React simplifies the creation of complex user interfaces by providing a declarative way to define the desired end state(s) of the UI.
  • Declarative means that you define the target user interface content and structure, combined with different states (e.g., "is a modal open or closed?"), and you leave it up to React to figure out the appropriate DOM instructions.
  • The react package itself derives UI states and manages a virtual DOM. It's "bridges" like react-dom or react-native that translate this virtual DOM into actual UI (DOM) instructions.
  • With React, you can build Single Page Applications (SPAs), meaning that React is used to control the entire user interface on all...

Introduction

In the previous chapter, you learned about React in general, what it is, and why you should consider using it for building user interfaces. You also learned how to create React projects with the help of Vite, by running npm create vite@latest <your-project-name>.

In this chapter, you will learn about one of the most important React concepts and building blocks. You will learn that components are reusable building blocks that are used to build user interfaces. In addition, JSX code will be discussed in greater detail so that you will be able to use the concept of components and JSX to build your own first basic React apps.

What Are Components?

A key concept of React is the usage of so-called components. Components are reusable building blocks that are combined to compose the final user interface. For example, a basic website could be made up of a sidebar that includes navigation items and a main section that includes elements for adding and viewing tasks.

Figure 2.1: An example task management screen with sidebar and main area

If you look at this example page, you might be able to identify various building blocks (i.e., components). Some of these components are even reused:

  • The sidebar and its navigation items
  • The main page area
  • In the main area, the header with the title and due date
  • A form for adding tasks
  • A list of tasks

Please note that some components are nested inside other components—i.e., components are also made up of other components. That’s a key feature of React and similar libraries.

Why Components?

No matter...

What Does React Do with All These Components?

If you follow the trail of all components and their import and export statements to the top, you will find a root.render(...) instruction in the main entry script of the React project. Typically, this main entry script can be found in the main.jsx file, located in the project’s src/ folder. This render() method, which is provided by the React library (to be precise, by the react-dom package), takes a snippet of JSX code and interprets and executes it for you.

The complete snippet you find in the root entry file (main.jsx) typically looks like this:

import React from 'react';
import ReactDOM from 'react-dom/client';
import './index.css';
import App from './App.jsx';
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<App />);

The exact code you find in your new React project might look slightly different.

It may, for instance, include...

JSX vs HTML vs Vanilla JavaScript

As mentioned above, React projects typically contain lots of JSX code. Most custom components will return JSX code snippets. You can see this in all the examples shared thus far, and you will see it in basically every React project you explore, no matter whether you are using React for the browser or other platforms like react-native.

But what exactly is this JSX code? How is it different from HTML? And how is it related to vanilla JavaScript?

JSX is a feature that’s not part of vanilla JavaScript. What can be confusing, though, is that it’s also not directly part of the React library.

Instead, JSX is syntactical sugar that is provided by the build workflow that’s part of the overall React project. When you start the development web server via npm run dev or build the React app for production (i.e., for deployment) via npm run build, you kick off a process that transforms this JSX code back to regular JavaScript instructions...

Moving Beyond Static Content

Thus far, in all these examples, the content that was returned was static. It was content like <p>Hello World!</p>—which of course is content that never changes. It will always output a paragraph that says, 'Hello World!'.

But most websites, of course, need to output dynamic content that may change (e.g., due to user input). Similarly, you’ll have a hard time finding lots of websites without any images.

Thus, as a React developer, it’s important to know how to output dynamic content (and what “dynamic content” actually means) and how to display images in a React app.

Outputting Dynamic Content

At this point in the book, you don’t yet have any tools to make the content more dynamic. To be precise, React requires that state concept (which will be covered in Chapter 4, Working with Events and State) to change the content that is displayed (e.g., upon user input or some other event...

When Should You Split Components?

As you work with React and learn more and more about it, and as you dive into more challenging React projects, you will most likely come up with one very common question: When should I split a single React component into multiple separate components?

As mentioned earlier in this chapter, React is all about components, and it is therefore very common to have dozens, hundreds, or even thousands of React components in a single React project.

When it comes to splitting a single React component into multiple smaller components, there is no hard rule you must follow. As mentioned earlier, you could put all your UI code into one single, large component. Alternatively, you could create a separate custom component for every single HTML element and piece of content that you have in your UI. Both approaches are probably not that great. Instead, a good rule of thumb is to create a separate React component for every data entity that can be identified.

...

Summary and Key Takeaways

  • React embraces components: reusable building blocks that are combined to define the final user interface
  • Components must return renderable content – typically, JSX code that defines the HTML code that should be produced in the end
  • React provides a lot of built-in components: besides special components like <>…</>, you get components for all standard HTML elements
  • To allow React to tell custom components apart from built-in components, custom component names have to start with capital letters when being used in JSX code (typically, PascalCase naming is used)
  • JSX is neither HTML nor a standard JavaScript feature – instead, it’s syntactical sugar provided by build workflows that are part of all React projects
  • You could replace JSX code with React.createElement(…) calls, but since this leads to significantly more unreadable code, it’s typically avoided
  • When using...

Apply What You Learned

With this and the previous chapter, you have all the knowledge you need to create a React project and populate it with some first, basic components.

Below, you’ll find your first two practical activities for this book.

Activity 2.1: Creating a React App to Present Yourself

Suppose you are creating your personal portfolio page, and as part of that page, you want to output some basic information about yourself (e.g., your name or age). You could use React and build a React component that outputs this kind of information, as outlined in the following activity.

The aim is to create a React app as you learned in the previous chapter (i.e., create it via npm create vite@latest <your-project-name> and run npm run dev to start the development server) and edit the App.jsx file such that you output some basic information about yourself. You could, for example, output your full name, address, job title, or other kinds of information. In the...

Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • Work through clear, concise explanations of core React 19 functionalities
  • Complete practical exercises that challenge you to build your own simple apps
  • Discover fullstack React with Next.js, React Server Components, Suspense, and more

Description

Maximilian Schwarzmüller is a bestselling instructor who has helped more than three million students worldwide learn how to code. His bestselling React video course, “React – The Complete Guide”, has over nine hundred thousand students on Udemy. Max has written this quick-start reference that distills the core concepts of React. Simple explanations, relevant examples, and step-by-step derivations make this guide the ideal resource for busy developers. In this second edition, Max guides you through changes brought by React 19, including the new use() hook, form actions, and how to think about React on the server. This book will support you through your next React projects in giving you a behind-the-scenes understanding of the framework – whether you've just finished Max's video course and are looking for a handy reference, or you’re using a variety of other learning materials and need a single study guide to bring everything together. You’ll find full solutions to all end-of-chapter quizzes and exercises in the book’s GitHub repository.

Who is this book for?

This React book is for developers who have prior experience with, or who are currently learning, the basics of React. You can use this book as a standalone resource to consolidate your understanding or as a companion guide to other courses. To get the most value from this book, you should have a basic understanding of the fundamentals of JavaScript, HTML, and CSS.

What you will learn

  • Build modern, user-friendly, and reactive web apps
  • Create components and utilize props to pass data between them
  • Handle events, perform state updates, and manage conditional content
  • Add styles dynamically and conditionally for modern user interfaces
  • Use advanced state management techniques such as React's Context API
  • Utilize React Router to render different pages for different URLs
  • Understand key best practices and optimization opportunities
  • Learn about React Server Components and Server Actions

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Jan 14, 2025
Length: 544 pages
Edition : 2nd
Language : English
ISBN-13 : 9781836202264
Languages :
Tools :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Product feature icon AI Assistant (beta) to help accelerate your learning
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Product Details

Publication date : Jan 14, 2025
Length: 544 pages
Edition : 2nd
Language : English
ISBN-13 : 9781836202264
Languages :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
€18.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
€189.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts
€264.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts

Table of Contents

20 Chapters
React – What and Why Chevron down icon Chevron up icon
Understanding React Components and JSX Chevron down icon Chevron up icon
Components and Props Chevron down icon Chevron up icon
Working with Events and State Chevron down icon Chevron up icon
Rendering Lists and Conditional Content Chevron down icon Chevron up icon
Styling React Apps Chevron down icon Chevron up icon
Portals and Refs Chevron down icon Chevron up icon
Handling Side Effects Chevron down icon Chevron up icon
Handling User Input & Forms with Form Actions Chevron down icon Chevron up icon
Behind the Scenes of React and Optimization Opportunities Chevron down icon Chevron up icon
Working with Complex State Chevron down icon Chevron up icon
Building Custom React Hooks Chevron down icon Chevron up icon
Multipage Apps with React Router Chevron down icon Chevron up icon
Managing Data with React Router Chevron down icon Chevron up icon
Server-side Rendering & Building Fullstack Apps with Next.js Chevron down icon Chevron up icon
React Server Components & Server Actions Chevron down icon Chevron up icon
Understanding React Suspense & The use() Hook Chevron down icon Chevron up icon
Next Steps and Further Resources Chevron down icon Chevron up icon
Other Books You May Enjoy Chevron down icon Chevron up icon
Index Chevron down icon Chevron up icon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.