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 3. Working with Properties

In this chapter, we will be exploring how to work with ReactJS properties. We're also going to learn how to integrate ReactJS with an external API (Facebook Open-Graph API) and render incoming data in a set of components. This chapter will cover the following items:

  • Component properties

  • Component's data flow

  • Configuring and consuming Facebook Open-Graph API

  • Creating a ReactJS component and list data from API

Component properties


In the Chapter 2, Exploring JSX and ReactJS Anatomy, we talked a lot about ReactJS properties and used them throughout our examples, but so far, we've just used them like HTML properties. They play a role that is far beyond that. It's common to use them to pass data through your components tree that defines your view; to pass configuration properties that come from parent components; to pass callbacks for user input, UI/custom events that need to be triggered outside, and so on.

Properties of a ReactJS component can't be changed once the component is rendered in the DOM.

Properties define the declarative interface of the component. In a h1 element that renders a name property, for example, you can't change this name once it's rendered, unless you create another instance of the component and render it in the same place in the DOM, replacing the old rendered component.

var GreetingsComponent = React.createClass({
  render: function() {
    return (
      <h1>Hello...

Configuring Facebook Open-Graph API


In the following sections, we're going to learn more about Facebook Open-Graph API and configure it so we can start crafting some code to build our awesome application.

What it is and how to configure it

Facebook Open-Graph API is a service for getting, editing, and adding common Facebook resources. Some of its functionalities that you can use in your own application are: login; request user-specific resource permissions such as manage events, post to friends walls, and the list goes on. It has a bunch of functionalities that you can use and integrate your app with. One of the main functionalities used by third-party applications is, of course, the login integration. You can use it just as a login platform, for instance, if you don't want or don't have time to build one.

Tip

The API documentation is provided at https://developers.facebook.com/ and it's recommended that you check this out.

If you want to test some requests to their API, without having to start...

Rendering data in a ReactJS component


We now have our data to pass to the ReactJS component we're going to create. First, let's start with the UserDetails component. This is going to show a link with the logged-in username and the source to this user Facebook page. First, remove our old logout anchor from the index.html file as this is not going to be necessary anymore. Our logout functionality will be moved to our ReactJS component instead. We'll also create another div, named user, above the main div; this new element will hold the UserDetails component. The changes in index.html should look like this:

<h1>Facebook User's list of likes</h1>
<div id="user"></div>
<div id="main"></div>

You can create the UserDetails ReactJS component at the bottom of the index.jsx file:

var UserDetails = React.createClass({
    handleLogout: function () {
    FB.logout(function () {
      alert("You're logged out, refresh the page in order to login again.");
    });
  }
...

Summary


In this chapter, we've learned how to pass properties to ReactJS components and render the UI based on those components. We have also learned how to make a parent component to communicate with its children. We have seen how to configure Facebook Open-Graph API, how to integrate that with ReactJS using login functionality, and how to render the response of an API request call into a set of smaller ReactJS components.

In the next chapter, we are going to dive into stateful components and explore how to make mutable ReactJS components based on a user's input or for any other reason that requires the state to change and your UI to represent that automatically.

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 €14.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