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
Free Learning
Arrow right icon
React and React Native
React and React Native

React and React Native: Build cross-platform JavaScript apps with native power for mobile, web and desktop

eBook
₹799 ₹3276.99
Paperback
₹4096.99
Subscription
Free Trial
Renews at ₹800p/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
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

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

React and React Native

Chapter 2. Rendering with JSX

This chapter will introduce you to JSX. We'll start by covering the basics: what is JSX? Then, you'll see that JSX has built-in support for HTML tags, as you would expect; so we'll run through a few examples here. After having looked at some JSX code, we'll discuss how it makes describing the structure of UIs easy for us. Then, we'll jump into building our own JSX elements, and using JavaScript expressions for dynamic content.

Ready?

What is JSX?

In this section, we'll implement the obligatory hello world JSX application. At this point, we're just dipping our toes into the water; more in-depth examples will follow. We'll also discuss what makes this syntax work well for declarative UI structures.

Hello JSX

Without further ado, here's your first JSX application:

// The "render()" function will render JSX markup and 
// place the resulting content into a DOM node. The "React" 
// object isn't explicitly used here, but it's used 
// by the transpiled JSX source. 
import React from 'react'; 
import { render } from 'react-dom'; 
 
// Renders the JSX markup. Notice the XML syntax 
// mixed with JavaScript? This is replaced by the 
// transpiler before it reaches the browser. 
render( 
  (<p>Hello, <strong>JSX</strong></p>), 
  document.getElementById('app') 
); 

Pretty simple, right? Let's walk through what's happening...

Just like HTML

At the end of the day, the job of a React component is to render HTML into the browser DOM. This is why JSX has support for HTML tags, out of the box. In this section, we'll look at some code that renders some of the available HTML tags. Then, we'll cover some of the conventions that are typically followed in React projects when HTML tags are used.

Built-in HTML tags

When we render JSX, element tags are referencing React components. Since it would be tedious to have to create components for HTML elements, React comes with HTML components. We can render any HTML tag in our JSX, and the output will be just as we'd expect. If you're not sure, you can always run the following code to see which HTML element tags React has:

// Prints a list of the global HTML tags 
// that React knows about. 
console.log( 
  'available tags', 
  Object.keys(React.DOM).sort() 
); 

You can see that React.DOM has all the built-in HTML elements that we need, implemented as...

Describing UI structures

JSX is the best way to describe complex UI structures. Let's look at some JSX markup that declares a more elaborate structure than a single paragraph:

import React from 'react'; 
import { render } from 'react-dom'; 
 
// This JSX markup describes some fairly-sophisticated 
// markup. Yet, it's easy to read, because it's XML and 
// XML is good for concisely-expressing hierarchical 
// structure. This is how we want to think of our UI, 
// when it needs to change, not as an individual element 
// or property. 
render(( 
  <section> 
    <header> 
      <h1>A Header</h1> 
    </header> 
    <nav> 
      <a href="item">Nav Item</a> 
    </nav> 
    <main> 
      <p>The main content...</p> 
    </main> 
    <footer> 
      <small>&copy; 2016</small> 
    </footer> 
  </section> 
  ), 
  document.getElementById(&apos...

Creating your own JSX elements

Components are the fundamental building blocks of React. In fact, components are the vocabulary of JSX markup. In this section, we'll see how to encapsulate HTML markup within a component. We'll build examples that show you how to nest custom JSX elements and how to namespace your components.

Encapsulating HTML

The reason that we want to create new JSX elements is so that we can encapsulate larger structures. This means that instead of having to type out complex markup, we just use our custom tag. The React component returns the JSX that replaces the element. Let's look at an example now:

// We also need "Component" so that we can 
// extend it and make a new JSX tag. 
import React, { Component } from 'react'; 
import { render } from 'react-dom'; 
 
// "MyComponent" extends "Component", which means that 
// we can now use it in JSX markup. 
class MyComponent extends Component { 
  render() { 
 ...

Using JavaScript expressions

As we saw in the preceding section, JSX has special syntax that lets us embed JavaScript expressions. Any time we render JSX content, expressions in the markup are evaluated. This is the dynamic aspect of JSX content, and in this section, you'll learn how to use expressions to set property values and element text content. You'll also learn how to map collections of data to JSX elements.

Dynamic property values and text

Some HTML property or text values are static, meaning that they don't change as the JSX is re-rendered. Other values, the values of properties or text, are based on data that's found elsewhere in the application. Remember, React is just the view layer. Let's look at an example so that you can get a feel for what the JavaScript expression syntax looks like in JSX markup:

import React from 'react'; 
import { render } from 'react-dom'; 
 
// These constants are passed into the JSX 
// markup using the JavaScript...

What is JSX?


In this section, we'll implement the obligatory hello world JSX application. At this point, we're just dipping our toes into the water; more in-depth examples will follow. We'll also discuss what makes this syntax work well for declarative UI structures.

Hello JSX

Without further ado, here's your first JSX application:

// The "render()" function will render JSX markup and 
// place the resulting content into a DOM node. The "React" 
// object isn't explicitly used here, but it's used 
// by the transpiled JSX source. 
import React from 'react'; 
import { render } from 'react-dom'; 
 
// Renders the JSX markup. Notice the XML syntax 
// mixed with JavaScript? This is replaced by the 
// transpiler before it reaches the browser. 
render( 
  (<p>Hello, <strong>JSX</strong></p>), 
  document.getElementById('app') 
); 

Pretty simple, right? Let's walk through what's happening here. First, we...

Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • • Build React and React Native applications using familiar component concepts
  • • Dive deep into each platform, from routing in React to creating native mobile applications that can run offline
  • • Use Facebook’s Relay, React and GraphQL technologies, to create a unified architecture that powers both web and native applications

Description

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

Who is this book for?

Perfect for anyone who wants to create mobile and desktop applications (including native mobile apps), React and React Native features step-by-step instructions and examples designed for all levels of React and React Native developers and students.

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Mar 08, 2017
Length: 500 pages
Edition : 1st
Language : English
ISBN-13 : 9781786469571
Category :
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
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Product Details

Publication date : Mar 08, 2017
Length: 500 pages
Edition : 1st
Language : English
ISBN-13 : 9781786469571
Category :
Languages :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
₹800 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
₹4500 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 ₹400 each
Feature tick icon Exclusive print discounts
₹5000 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 ₹400 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total 11,396.97
Mastering React Native
₹3649.99
React and React Native
₹4096.99
React Design Patterns and Best Practices
₹3649.99
Total 11,396.97 Stars icon
Banner background image

Table of Contents

26 Chapters
1. Why React? Chevron down icon Chevron up icon
2. Rendering with JSX Chevron down icon Chevron up icon
3. Understanding Properties and State Chevron down icon Chevron up icon
4. Event Handling – The React Way Chevron down icon Chevron up icon
5. Crafting Reusable Components Chevron down icon Chevron up icon
6. The React Component Lifecycle Chevron down icon Chevron up icon
7. Validating Component Properties Chevron down icon Chevron up icon
8. Extending Components Chevron down icon Chevron up icon
9. Handling Navigation with Routes Chevron down icon Chevron up icon
10. Server-Side React Components Chevron down icon Chevron up icon
11. Mobile-First React Components Chevron down icon Chevron up icon
12. Why React Native? Chevron down icon Chevron up icon
13. Kickstarting React Native Projects Chevron down icon Chevron up icon
14. Building Responsive Layouts with Flexbox Chevron down icon Chevron up icon
15. Navigating Between Screens Chevron down icon Chevron up icon
16. Rendering Item Lists Chevron down icon Chevron up icon
17. Showing Progress Chevron down icon Chevron up icon
18. Geolocation and Maps Chevron down icon Chevron up icon
19. Collecting User Input Chevron down icon Chevron up icon
20. Alerts, Notifications, and Confirmation Chevron down icon Chevron up icon
21. Responding to User Gestures Chevron down icon Chevron up icon
22. Controlling Image Display Chevron down icon Chevron up icon
23. Going Offline Chevron down icon Chevron up icon
24. Handling Application State Chevron down icon Chevron up icon
25. Why Relay and GraphQL? Chevron down icon Chevron up icon
26. Building a Relay React App Chevron down icon Chevron up icon

Customer reviews

Rating distribution
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
(1 Ratings)
5 star 0%
4 star 100%
3 star 0%
2 star 0%
1 star 0%
Caleb Nov 06, 2017
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
Content is really good, but all pages are black and gray. So, it is hard to see well when you read, I personally recommend reader with color pictures which provided by them. Actually, when I read this book, I had an intermediate level of React Native, also I finished React Native lectures on Udemy, but this is really focused on how it works, and more detail information comparing the Udemy lectures. So, it would be great if you want to know more and use a reference book. However, React Native is changing quickly, and it can be so out-dated easily, but I think it includes the most recent things about React Native. If this book published in color, it would be great to me...haha..
Amazon Verified review Amazon
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.