Reader small image

You're reading from  Getting Started with React

Product typeBook
Published inApr 2016
Reading LevelIntermediate
Publisher
ISBN-139781783550579
Edition1st Edition
Languages
Tools
Right arrow
Authors (3):
Doel Sengupta
Doel Sengupta
author image
Doel Sengupta

Doel Sengupta is a software programmer and is working in the industry for over 7 years, as a DevOps engineer and as a developer building enterprise level Web and mobile applications using RubyonRails and Rhomobile, Chef. Currently she is exploring the Javascript ecosystem. She has been a speaker in Ruby conferences. She finds interest in life sciences and has publications of her work in customised human joint prostheses design using Ansys & Mimics. She is an avid blogger (www.doels.net) writing about her technical and not-so-technical passions like culinary, photography, films. Follow her on twitter @doelsengupta.
Read more about Doel Sengupta

Manu Singhal
Manu Singhal
author image
Manu Singhal

Manu Singhal has been a programmer for 8 years and loves to code on Ruby and React. These days, he is busy cofounding a startup in e-commerce. In earlier roles, he has developed many enterprise and consumer based web/mobile applications and has also been a speaker at Ruby Conferences in India and the USA. He never misses a chance to play tennis and go hiking. He has worked with Tata Consultancy Services and McKinsey & Company as a software developer and an architect. He has contributed in books on Rhomobile and RubyMotion by Packt earlier.
Read more about Manu Singhal

Danillo Corvalan
Danillo Corvalan
author image
Danillo Corvalan

Danillo Corvalan is a software engineer who is passionate about software patterns and practices. He has a keen interest in the rapidly changing world of software development. He is quite insistent about the need of fast and reliable frameworks. He is originally from Brazil, now living in Amsterdam, the Netherlands. He loves biking a lot. In Brazil, he worked on applications for the general public and lawyers, at the Court of Justice in his hometown city, Cuiab/MT. Then, he moved to Florianpolis/SC, and worked at Bravi Software for developing hybrid and responsive web apps for education. Now, in Amsterdam, he is working at Vigour.io and helping to develop live multiscreen and responsive apps. From the web client-side perspective, in general, he has been in touch with technologies, such as vanilla JavaScript, jQuery, Backbone, and ReactJS. For the past 5 years, Danillo has also worked with open source platforms and JavaScript on the server side (Node.js). He has played with React Native in order to develop native mobile applications with ReactJS.
Read more about Danillo Corvalan

View More author details
Right arrow

Chapter 2. Exploring JSX and the ReactJS Anatomy

In this chapter, you are going to explore the JSX syntax and learn what it is and why it makes it easier for us to understand UI components. You will learn about the ReactJS anatomy and code some common scenarios in order to demonstrate this efficient syntax so that we can step forward in next chapters and build a full application. This chapter will walk you through the following topics:

  • What is JSX?

  • The ReactJS anatomy

  • JSX Gotchas

What is JSX?


JSX is a JavaScript syntax extension that looks similar to XML. It is used to build UI components in ReactJS. It's very similar to HTML with some subtle differences. JSX extends JavaScript in such a way that you can easily build ReactJS components with the same understanding as building HTML pages. It's commonly mixed with your JavaScript code because ReactJS thinks about UI in a different way. This paradigm will be explained later in the chapter.

It's wrong to say that you are mixing up your HTML with JavaScript. As already said, JSX extends JavaScript. Actually, you're not writing HTML tags, but you're writing JavaScript objects in the JSX syntax. Of course, it has to be transformed into plain JavaScript first.

When you write this example:

var HelloWorld = React.createClass({
  render: function () {
    return <h1>Hello World from Learning ReactJS</h1>;
  }
});

It's transformed into this:

var HelloWorld = React.createClass({
  render: function () {
    return React...

Why JSX?


In general, HTML and JavaScript are segregated in frameworks by defining UI or a view to represent their mutable data, which normally is a template language and/or a display logic interpreter. The following is some jQuery code:

<html>
  <head>
    <title>Just an example</title>
  </head>
  <body>
    <div id="my-awesome-app">
      <!-- Here go my rendered template -->
    </div>
    
    <script id="my-list" type="text/html">
      <ul>
        {{each items}}
          <li>
            ${name}
          </li>
        {{/each}}
      </ul>
    </script>
  </body>
</html>

The script element represents a template component that will be rendered in the my-awesome-app div element. The code here is a JavaScript file that pushes data to that template and asks jQuery to do the job and render the UI:

$("#my-list").tmpl(serverData).appendTo("#my-my-awesome-app");

Whenever you want to put some...

The ReactJS anatomy


Before going any further into JSX, we need to understand some basic rules to build ReactJS components. First, we're going to detail the basic methods that you've already used to create and render components. Then, we'll move to some rules to create them, and finally, we'll talk about children components.

Creating a component

In order to create a component, we need to use the React.createClass function. ReactJS components are basically classes. This method returns a ReactJS component definition that has a method named render, which is mandatory to implement. There are many other methods to configure your component and change its behavior that we are going to cover throughout the book.

This is an example of how to use the createClass and render method:

var HelloMessage = React.createClass({
  render: function() {
    return (
      <h1>Have a good day!</h1>
    );
  }
});

Note

It's a good practice to name all the classes and components in PascalCase. In addition to...

Learning JSX and Gotchas


Now it's time to master JSX and learn some Gotchas. You're going to learn some basic concepts to build ReactJS UI components using JSX. It includes practices when writing expressions, conditions, and creating lists of components. It will also walk you through how JSX differs from HTML (because it's not HTML) in some aspects.

Expressions

Consider the following code:

var Clock = React.createClass({
  render: function () {
    var today = new Date();
    return <h1>The time is { today.toLocaleTimeString() }</h1>;
  }
});

React.render(<Clock />, document.body);

JSX understands the curly braces {} whenever you want to put JavaScript code within your presentation code.

In the next example, let's improve our Clock component by supporting greetings depending on what the time is.

In the highlighted code mentioned later, if the current hour is lesser than 4, it should return day, and if the hour is greater than 4 but less than 18, it should return night:

var GreetingsClock...

Summary


In this chapter, you learned what JSX is, its syntax, and why it is necessary. We looked into some examples and how to build them using JSX. We covered very basic principles of ReactJS and how JSX helps you build components faster, easy to read, and reasonable.

In the next chapter, we are going to dive into ReactJS properties and how to break the UI into smaller components. You are going to learn it by creating a small application that will consume the Facebook Open Graph API and list your liked pages.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Getting Started with React
Published in: Apr 2016Publisher: ISBN-13: 9781783550579
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.
undefined
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 $15.99/month. Cancel anytime

Authors (3)

author image
Doel Sengupta

Doel Sengupta is a software programmer and is working in the industry for over 7 years, as a DevOps engineer and as a developer building enterprise level Web and mobile applications using RubyonRails and Rhomobile, Chef. Currently she is exploring the Javascript ecosystem. She has been a speaker in Ruby conferences. She finds interest in life sciences and has publications of her work in customised human joint prostheses design using Ansys & Mimics. She is an avid blogger (www.doels.net) writing about her technical and not-so-technical passions like culinary, photography, films. Follow her on twitter @doelsengupta.
Read more about Doel Sengupta

author image
Manu Singhal

Manu Singhal has been a programmer for 8 years and loves to code on Ruby and React. These days, he is busy cofounding a startup in e-commerce. In earlier roles, he has developed many enterprise and consumer based web/mobile applications and has also been a speaker at Ruby Conferences in India and the USA. He never misses a chance to play tennis and go hiking. He has worked with Tata Consultancy Services and McKinsey & Company as a software developer and an architect. He has contributed in books on Rhomobile and RubyMotion by Packt earlier.
Read more about Manu Singhal

author image
Danillo Corvalan

Danillo Corvalan is a software engineer who is passionate about software patterns and practices. He has a keen interest in the rapidly changing world of software development. He is quite insistent about the need of fast and reliable frameworks. He is originally from Brazil, now living in Amsterdam, the Netherlands. He loves biking a lot. In Brazil, he worked on applications for the general public and lawyers, at the Court of Justice in his hometown city, Cuiab/MT. Then, he moved to Florianpolis/SC, and worked at Bravi Software for developing hybrid and responsive web apps for education. Now, in Amsterdam, he is working at Vigour.io and helping to develop live multiscreen and responsive apps. From the web client-side perspective, in general, he has been in touch with technologies, such as vanilla JavaScript, jQuery, Backbone, and ReactJS. For the past 5 years, Danillo has also worked with open source platforms and JavaScript on the server side (Node.js). He has played with React Native in order to develop native mobile applications with ReactJS.
Read more about Danillo Corvalan