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

Understanding React Components and Hooks

In this chapter, we will delve into the React components and their fundamental aspects and introduce you to the power of Hooks.

We will explore the essential concept of component data and how it shapes the structure of your React applications. We will discuss two primary types of component data: properties and state. Properties allow us to pass data to components, while state enables components to manage and update their internal data dynamically. We will examine how these concepts apply to function components and illustrate the mechanics of setting component state and passing properties.

In this chapter, we’ll cover the following topics:

  • Introduction to React components
  • What are component properties?
  • What is component state?
  • React Hooks
  • Maintaining state using Hooks
  • Performing initialization and cleanup actions
  • Sharing data using context Hooks
  • Memoization with Hooks
...

Technical requirements

Introduction to React components

React components are the building blocks of modern web and mobile applications. They encapsulate reusable sections of code that define the structure, behavior, and appearance of different parts of a user interface. By breaking down the UI into smaller, self-contained components, React enables developers to create scalable, maintainable, and interactive applications.

At its core, a React component is a JavaScript function or class that returns JSX syntax, which resembles HTML markup. In this book, we will focus mostly on function components, as they have become the preferred approach for building components in recent years. Function components are simpler, more concise, and easier to understand compared to class components. They leverage the power of JavaScript functions and utilize React Hooks to manage state and perform side effects.

One of the primary advantages of using components in React is their reusability. Components can be reused across...

What are component properties?

In React, component properties, commonly known as props, allow us to pass data from a parent component to its child components. Props provide a way to customize and configure components, making them flexible and reusable. Props are read-only, meaning that the child component should not modify them directly. Instead, the parent component can update the props value and trigger a re-render of the child component with the updated data.

When defining a function component, you can access the props passed to it as a parameter:

const MyComponent = (props) => {
  return (
    <div>
      <h1>{props.title}</h1>
      <p>{props.description}</p>
    </div>
  );
};

In the above example, the MyComponent function component receives the props object as a parameter. We can access the individual props by using dot notation, such as props.title and props.description, to render the data within the component’s JSX...

What is component state?

In React, component state refers to the internal data held by a component. It represents the mutable values that can be used within the component and can be updated over time. State allows components to keep track of information that can change, such as user input, API responses, or any other data that needs to be dynamic and responsive.

State is a feature provided by React that enables components to manage and update their own data. It allows components to re-render when the state changes, ensuring that the user interface reflects the latest data.

To define state in a React component, you should use the useState hook inside of the component. You can then access and modify the state within the component’s methods or JSX code. When the state is updated, React will automatically re-render the component and its child components to reflect the changes.

Before jumping to examples of using state in components, let’s briefly explore what...

React Hooks

React Hooks are a feature introduced in React 16.8 that allows you to use state and other React features in functional components. Before Hooks, state management and lifecycle methods were primarily used in class components. Hooks provide a way to achieve similar functionality in functional components, making them more powerful and easier to write and understand.

Hooks are functions that enable you to “hook into” React’s internal features, such as state management, context, effects, and more. They are prefixed with the use keyword (such as useState, useEffect, useContext, and so on). React provides several built-in Hooks, and you can also create custom Hooks to encapsulate reusable stateful logic.

The most commonly used built-in Hooks are:

  • useState: This hook allows you to add state to a functional component. It returns an array with two elements: the current state value and a function to update the state.
  • useEffect: This hook...

Performing initialization and cleanup actions

Often, our React components need to perform actions when the component is created. For example, a common initialization action is to fetch the API data that the component needs. Another common action is to make sure that any pending API requests are canceled when the component is removed. In this section, you’ll learn about the useEffect hook and how it can help you with these two scenarios. You’ll also learn how to make sure that the initialization code doesn’t run too often.

Fetching component data

The useEffect hook is used to run “side effects” in your component. Another way to think about side-effect code is that functional components have only one job: returning JSX content to render. If the component needs to do something else, such as fetching API data, this should be done in a useEffect hook. For example, if you were to just make the API call as part of your component function, you would...

Before you begin: Join our book community on Discord

Give your feedback straight to the author himself and chat to other early readers on our Discord server (find the "react-and-react-native-5e" channel under EARLY ACCESS SUBSCRIPTION).

https://packt.link/EarlyAccess

Qr code Description automatically generated

In this chapter, we will delve into the React components and their fundamental aspects and introduce you to the power of Hooks. We will explore the essential concept of component data and how it shapes the structure of your React applications. We will discuss two primary types of component data: properties and state. Properties allow us to pass data to components, while state enables components to manage and update their internal data dynamically. We will examine how these concepts apply to function components and illustrate the mechanics of setting component state and passing properties.In this chapter, we'll cover the following topics:

  • Introduction to React Components
  • What are component properties?
  • What...

Technical requirements

The code for this chapter can be found here: https://github.com/PacktPublishing/React-and-React-Native-5E/tree/main/Chapter03.

Introduction to React Components

React components are the building blocks of modern web and mobile applications. They encapsulate reusable sections of code that define the structure, behavior, and appearance of different parts of a user interface. By breaking down the UI into smaller, self-contained components, React enables developers to create scalable, maintainable, and interactive applications.At its core, a React component is a JavaScript function or class that returns JSX syntax, which resembles HTML markup. In this book, we will focus mostly on function components, as they have become the preferred approach for building components in recent years. Function components are simpler, more concise, and easier to understand compared to class components. They leverage the power of JavaScript functions and utilize React Hooks to manage state and perform side effects.One of the primary advantages of using components in React is their reusability. Components can be reused across multiple...

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}