Book Image

React and React Native

By : Adam Boduch
Book Image

React and React Native

By: Adam Boduch

Overview of this book

para 1: Dive into the world of React and create powerful applications with responsive and streamlined UIs! With React best practices for both Android and iOS, this book demonstrates React and React Native in action, helping you to create intuitive and engaging applications. Para 2: React and React Native allow you to build desktop, mobile and native applications for all major platforms. Combined with Flux and Relay, you?ll be able to create powerful and feature-complete applications from just one code base. Para 3: Discover how to build desktop and mobile applications using Facebook?s innovative UI libraries. You?ll also learn how to craft composable UIs using React, and then apply these concepts to building Native UIs using React Native. Finally, find out how you can create React applications which run on all major platforms, and leverage Relay for feature-complete and data-driven applications. Para 4: What?s Inside ? Craft composable UIs using React & build Native UIs using React Native ? Create React applications for major platforms ? Access APIs ? Leverage Relay for data-driven web & native mobile applications
Table of Contents (34 chapters)
React and React Native
Credits
About the Author
About the Reviewer
www.PacktPub.com
Customer Feedback
Dedication
Preface

Performance matters


Using React to build user interfaces means that we can declare the structure of the UI with JSX. This is less error-prone than the imperative approach to assembling the UI piece by piece. However, the declarative approach does present us with one challenge: performance.

For example, having a declarative UI structure is fine for the initial rendering, because there's nothing on the page yet. So, the React renderer can look at the structure declared in JSX, and render it into the browser DOM. This is illustrated in the following diagram:

On the initial render, React components and their JSX are no different from other template libraries. For instance, Handlebars will render a template to HTML markup as a string, which is then inserted into the browser DOM. Where React is different from libraries such as Handlebars, is when data changes and we need to re-render the component. Handlebars will just rebuild the entire HTML string, the same way it did on the initial render. Since this is problematic for performance, we often end up implementing imperative workarounds that manually update tiny bits of the DOM. What we end up with is a tangled mess of declarative templates and imperative code to handle the dynamic aspects of the UI.

We don't do this in React. This is what sets React apart from other view libraries. Components are declarative for the initial render, and they stay this way even as they're re-rendered. It's what React does under the hood that makes re-rendering declarative UI structures possible.

React has something called the virtual DOM, which is used to keep a representation of the real DOM elements in memory. It does this so that each time we re-render a component, it can compare the new content, to the content that's already displayed on the page. Based on the difference, the virtual DOM can execute the imperative steps necessary to make the changes. So not only do we get to keep our declarative code when we need to update the UI, React will also make sure that it's done in a performant way. Here's what this process looks like:

Note

When you read about React, you'll often see words like diffing and patching. Diffing means comparing old content with new content to figure out what's changed. Patching means executing the necessary DOM operations to render the new content.