<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Samer Buna</title>
    <description>The latest articles on Forem by Samer Buna (@samerbuna).</description>
    <link>https://forem.com/samerbuna</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F52592%2F1b84cb86-c2dc-4a08-9911-4ebe6fe2802d.jpg</url>
      <title>Forem: Samer Buna</title>
      <link>https://forem.com/samerbuna</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/samerbuna"/>
    <language>en</language>
    <item>
      <title>GraphQL In Action: Introduction</title>
      <dc:creator>Samer Buna</dc:creator>
      <pubDate>Thu, 05 Mar 2020 20:21:00 +0000</pubDate>
      <link>https://forem.com/samerbuna/graphql-in-action-introduction-233a</link>
      <guid>https://forem.com/samerbuna/graphql-in-action-introduction-233a</guid>
      <description>&lt;p&gt;&lt;em&gt;What is GraphQL. What are its design concepts. How is it different from its alternatives. What are its advantages and disadvantages.&lt;/em&gt;&lt;/p&gt;




&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--3idYza3Z--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1600/1%2AF9NSC-2qROrjOvM9olpA_g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--3idYza3Z--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1600/1%2AF9NSC-2qROrjOvM9olpA_g.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;I am writing the &lt;a href="http://bit.ly/graphql-in-action"&gt;GraphQL In Action&lt;/a&gt; book for Manning and 6 chapters (out of 11) has been published to MEAP.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Below is the entire first chapter of the book. Feedback is welcome and very much appreciated.&lt;/strong&gt;&lt;/p&gt;




&lt;p&gt;This chapter covers&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What GraphQL is and the design concepts behind it&lt;/li&gt;
&lt;li&gt;How GraphQL is different from its alternatives, like REST APIs&lt;/li&gt;
&lt;li&gt;The language and service parts of a GraphQL system&lt;/li&gt;
&lt;li&gt;The advantages and disadvantages of GraphQL&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Necessity is the mother of invention. The product that inspired the creation of GraphQL was invented at Facebook because they needed to solve many technical issues with their mobile application. However, I think GraphQL became so popular so fast not because it solves technical problems but rather because it solves communication problems.&lt;/p&gt;

&lt;p&gt;Communication is hard. Improving our communication skills makes our lives better on many levels and, similarly, improving the communication between the different parts of a software application makes that application easier to understand, develop, maintain, and scale.&lt;/p&gt;

&lt;p&gt;That's really why I think GraphQL is a game-changer. It changed the game of how the different "ends" of a software application (front-end and back-end) communicate with each other. It gave equal powers to them, it made them independent of each other, it decoupled their communication process from its underlying technical transport channel, and it introduced a new rich language in a place where the common previously spoken language was limited to just a few words.&lt;/p&gt;

&lt;p&gt;GraphQL powers many applications at Facebook today, including the main web application at facebook.com, the Facebook mobile application, and Instagram. Developers' interest in GraphQL is very clear and GraphQL's adoption is growing fast. Besides Facebook, GraphQL is used in many other major web and mobile applications like GitHub, Yelp, Pinterest, Twitter, The New York Times, Coursera, and Shopify. Given that GraphQL is a young technology, this is an impressive list.&lt;/p&gt;

&lt;p&gt;In this first chapter, let's learn what exactly is GraphQL, what problems it solves, and what problems it introduces!&lt;/p&gt;

&lt;h2&gt;
  
  
  What is GraphQL
&lt;/h2&gt;

&lt;p&gt;The word "graph" in GraphQL comes from the fact that the best way to represent data in the real world is with a graph data structure. If you analyze any data model, big or small, you'll always find it to be a graph of objects with many relations between them.&lt;/p&gt;

&lt;p&gt;That was the first "aha" moment for me when I started learning about GraphQL. Why think of data in terms of resources (in URLs) on even join tables when you can beautifully think of it as a graph?&lt;/p&gt;

&lt;p&gt;The "QL" in GraphQL might be a bit confusing though. Yes, GraphQL is a "Query Language" for data APIs but that's from the perspective of only the frontend consumer of these data APIs. GraphQL is also a runtime layer that needs to be implemented on the back-end and that layer is what makes the front-end consumer able to use the new "language".&lt;/p&gt;

&lt;p&gt;The GraphQL "language" is designed to be declarative and efficient. Developers of data APIs consumers (like mobile and web applications) can use that language to request their exact data needs in a language close to how they think about data in their heads instead of the language related to how the data is stored or how data relations are implemented.&lt;/p&gt;

&lt;p&gt;On the back-end, GraphQL needs a runtime. That runtime provides a structure for servers to describe the data to be exposed in their APIs. This structure is what we call a "schema" in the GraphQL world.&lt;/p&gt;

&lt;p&gt;Any client can then use the GraphQL language to construct a text that represents their exact data needs using the back-end schema. The client then sends that text to the API service through a transport channel (for example, HTTP). The GraphQL runtime layer accepts that text request and then communicates with other services in the backend stack to put together a suitable data response for the text request. It'll then send that data back to the consumer in a format like JSON.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--_nIM7WAl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://jscomplete.com/images/reads/gia/ch01_fig_01_gqlia.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--_nIM7WAl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://jscomplete.com/images/reads/gia/ch01_fig_01_gqlia.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;
GraphQL is a language and a runtime



&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;NOTE&lt;/strong&gt;: GraphQL is not specific to any back-end or front-end framework, technical stack, or database. It can be used in any frontend environment, on any backend platform, and with any database engine. You can use it on any transport channel and make it use any data representation format.&lt;/p&gt;

&lt;p&gt;In frontend Web or Mobile applications, you can use GraphQL with a client like Apollo or Relay or by manually making Ajax calls to a GraphQL server. You can use a library like React (or React Native) to manage how your views use the data coming from a GraphQL service but you can also do that with APIs native to their UI environments as well (like the DOM API or the native iOS components).&lt;/p&gt;

&lt;p&gt;While you do not need React, Apollo, or Relay to use GraphQL in your applications, these libraries add more value to how you can leverage GraphQL APIs without having to do complex data management tasks.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  The Big Picture
&lt;/h3&gt;

&lt;p&gt;An API, in general, is an interface that enables the communication between multiple components in an application. For example, an API can enable the communication that needs to happen between a web client and a database server. The client needs to tell the server what data it needs and the server needs to fulfill this client's requirement with objects representing the data they asked for.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--JQNH59pe--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://jscomplete.com/images/reads/gia/ch01_fig_02_gqlia.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--JQNH59pe--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://jscomplete.com/images/reads/gia/ch01_fig_02_gqlia.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;
The big picture of data APIs



&lt;p&gt;There are different types of APIs and every big application needs them. When talking about GraphQL, we are specifically talking about the API type that is used to read and modify data, which is usually referred to as a "Data API".&lt;/p&gt;

&lt;p&gt;GraphQL is one option out of many that can be used to provide applications with programmable interfaces to read and modify the data they need from data services. Other options include REST, SOAP, XML, and even SQL itself.&lt;/p&gt;

&lt;p&gt;SQL (the Standard Query Language) might be directly compared to GraphQL because "QL" is in both names, after all. Both SQL and GraphQL provide a language to query data schemas. They can both be used to read and modify data.&lt;/p&gt;

&lt;p&gt;For example, assuming that we have a table of data about a company's employees, the following can be an example SQL statement to read data about employees in one department:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;first_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;last_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;birth_date&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;hire_date&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;employees&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;department&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'ENGINEERING'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
SQL statement for querying





&lt;p&gt;Here is another example SQL statement that can be used to insert data for a new employee:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="k"&gt;INTO&lt;/span&gt; &lt;span class="n"&gt;employees&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;first_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;last_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;birth_date&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;hire_date&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;VALUES&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'John'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'Doe'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'john@doe.name'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'01/01/1990'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'01/01/2020'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
SQL statement for mutating





&lt;p&gt;You can use SQL to communicate data operations like we did above. The relational databases that these SQL statements are sent to usually support different formats for their responses. Each SQL operation type will have a different response. A SELECT operation might return a single row or multiple rows. An INSERT operation might return just a confirmation, the inserted rows, or an error response.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;TIP&lt;/strong&gt;: While possible, SQL would not be a good language to use for communicating data requirements directly by mobile and web applications. SQL is simply too powerful and too flexible and it would introduce many challenges. For example, exposing your exact database structure publicly would be a challenging security problem. You can put SQL behind another service layer but that means you need to come up with a parser and analyzer to perform operations on users' SQL queries before sending them to the database. That parser/analyzer is something that you get out of the box with any GraphQL server implementation.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;While SQL is directly supported by most relational databases, GraphQL is its own thing. GraphQL needs a runtime service of its own. You cannot just start querying databases using the GraphQL query language (at least not yet). You will need to use a service layer that supports GraphQL or implement one yourself.&lt;/p&gt;

&lt;p&gt;JSON is a language that can be used to communicate data. Here is a JSON text that can represent John's data:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "data": {
    "employee":{
      id: 42,
      name: "John Doe",
      email: "john@doe.name",
      birthDate: "01/01/1990",
      hireDate: "01/01/2020"
    }
  }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
JSON object representing data





&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;NOTE&lt;/strong&gt;: Note how the data communicated about John does not have to be in the exact "structure" of how it is saved in the database. I used camel-case property names and I combined first_name and last_name into one name field.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;JSON is a popular language to communicate data from API servers to client applications. Most of the modern data API servers use JSON to fulfill the data requirements of a client application. GraphQL servers are no exception; JSON is the popular choice to fulfill the requirements of GraphQL data requests.&lt;/p&gt;

&lt;p&gt;JSON can also be used by client applications to communicate their data requirements to API servers. For example, here is a possible JSON object that can be used to communicate the data requirement for the employee object response:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "select": {
    "fields": ["name", "email", "birthDate", "hireDate"],
    "from": "employees",
    "where": {
      "id": {
       "equals": 42
      }
    }
  }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
JSON example for querying





&lt;p&gt;GraphQL for client applications is another language they can use to express their data requirements. The following is how the same previous data requirement can be expressed with a GraphQL query:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight graphql"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="n"&gt;employee&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="n"&gt;birthDate&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="n"&gt;hireDate&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
GraphQL example for querying





&lt;p&gt;The GraphQL query above represents the same data need as the JSON object, but as you can see it has a different and shorter syntax. A GraphQL server can be made to understand this syntax and translate it into what the actual data storage engine can understand (for example, it would translate it into SQL statements for a relational database). Then, the GraphQL server can take what the storage engine responds with and translate it into something like JSON or XML and send it back to the client application.&lt;/p&gt;

&lt;p&gt;This is nice because no matter what storage engine (or multiple storage engines) you have to deal with, with GraphQL you make API servers and client applications both work with a universal language for requests and a universal language for responses.&lt;/p&gt;

&lt;p&gt;In a nutshell, GraphQL is all about optimizing data communication between a client and a server. This includes the client asking for the needed data and communicating that need to the server, the server preparing a fulfillment for that need and communicating that fulfillment back to the client. GraphQL allows clients to ask for the exact data they need and make it easier for servers to aggregate data from multiple data storage resources.&lt;/p&gt;

&lt;p&gt;At the core of GraphQL, there is a strong type system that is used to describe the data and organize the APIs. This type system gives GraphQL many advantages on both the server and client sides. Types ensure that the clients ask for only what is possible and provide clear and helpful errors. Clients can use types to minimize any manual parsing of data elements. GraphQL type system allows for rich features like having an introspective API and being able to build powerful tools for both clients and servers. One of the popular GraphQL tools that relies on this concept is called GraphiQL, which is a feature-rich browser-based editor to explore and test GraphQL requests. You will learn about GraphiQL in the next chapter.&lt;/p&gt;

&lt;h3&gt;
  
  
  GraphQL is a specification
&lt;/h3&gt;

&lt;p&gt;Although Facebook engineers started working on GraphQL in 2012, it was 2015 when they released a public specifications document for it. You can see the current version of this document by navigating to &lt;a href="https://jscomplete.com/graphql-spec"&gt;jscomplete.com/graphql-spec&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;This document is maintained by a community of companies and individuals on GitHub. GraphQL is still an evolving language, but the specifications document was a genius start for the project because it defined standard rules and practices that all implementers of GraphQL runtimes need to adhere to. There have been many implementations of GraphQL libraries in many different programming languages and all of them closely follow the specification document and update their implementations when that document is updated. If you work on a GraphQL project in Ruby and later switch to another project in Scala, the syntax will change but the rules and practices will remain the same.&lt;/p&gt;

&lt;p&gt;You can ultimately learn EVERYTHING about the GraphQL language and runtime requirements in that official specification document. It is a bit technical but you can still learn a lot from it by reading its introductory parts and examples. This book will not cover each and everything in that document, so I recommend that you skim through it once you are done with the book.&lt;/p&gt;

&lt;p&gt;The specification document starts by describing the syntax of the GraphQL language. Let's talk about that first.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;NOTE&lt;/strong&gt;: Alongside the specification document, Facebook also released a reference implementation library for GraphQL runtimes in JavaScript. JavaScript is the most popular programming language and the one closest to mobile and web applications, which are two of the popular channels where using GraphQL can make a big difference. The reference JavaScript implementation of GraphQL is hosted at &lt;a href="https://github.com/graphql/graphql-js"&gt;github.com/graphql/graphql-js&lt;/a&gt; and it's the one we will be using in this book. I'll refer to this implementation as "GraphQL.js".&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  GraphQL is a language
&lt;/h3&gt;

&lt;p&gt;While the Q (for query) is right there in the name, querying is associated with reading but GraphQL can be used for both reading and modifying data. When you need to read data with GraphQL you use queries and when you need to modify data you use mutations. Both queries and mutations are part of the GraphQL language.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;NOTE&lt;/strong&gt;: Besides &lt;strong&gt;queries&lt;/strong&gt; and &lt;strong&gt;mutations&lt;/strong&gt;, Graphql also supports a third request type which is called a &lt;strong&gt;subscription&lt;/strong&gt; and it's used for real-time data monitoring requests.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This is just like how you use SELECT statements to read data with SQL and you use INSERT, UPDATE, and DELETE statements to modify it. The SQL language has certain rules that you must follow. For example, a SELECT statement requires a FROM clause and can optionally have a WHERE clause. Similarly, the GraphQL language has certain rules that you must follow as well. For example, a GraphQL query must have a name or be the only query in a request. You will learn about the rules of the GraphQL language in the next few chapters.&lt;/p&gt;

&lt;p&gt;A query language like GraphQL (or SQL) is different from programming languages like JavaScript or Python. You cannot use the GraphQL language to create User Interfaces or perform complex computations. Query languages have more specific use cases and they often require the use of other programming languages to make them work. Nevertheless, I would like you to first think of the query language concept by comparing it to programming languages and even to the languages that we speak, like English. This is a very limited-scope comparison, but I think in the case of GraphQL it will make you understand and appreciate a few things about it.&lt;/p&gt;

&lt;p&gt;The evolution of programming languages in general is making them closer and closer to the human languages that we speak. Computers used to only understand imperative instructions and that is why we have been using imperative paradigms to program them. However, computers today are starting to understand declarative paradigms and you can program them to understand &lt;em&gt;wishes&lt;/em&gt;. Declarative programming has many advantages (and disadvantages), but what makes it such a good idea is that we always prefer to reason about problems in declarative ways. Declarative thinking is easy for us.&lt;/p&gt;

&lt;p&gt;We can use the English language to declaratively communicate data needs and fulfillments. For example, imagine that John is the client and Jane is the server. Here is an English data communication session:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;John: &lt;em&gt;Hey Jane, how long does it take the sun light to reach planet earth?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Jane: &lt;em&gt;A bit over 8 minutes.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;John: &lt;em&gt;How about the light from the moon?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Jane: &lt;em&gt;A bit under 2 seconds.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;John can also easily ask both questions in one sentence and Jane can easily answer them both by adding more words to her answer.&lt;/p&gt;

&lt;p&gt;When we communicate using the English language, we understand special expressions like "a bit over" and "a bit under". Jane also understood that the incomplete second question is related to the first one. Computers, on the other hand, are not very good (yet) at understanding things from the context. They need more structure.&lt;/p&gt;

&lt;p&gt;GraphQL is just another declarative language that John and Jane can use to do that data communication session. It is not as good as the English language, but it is a structured language that computers can easily parse and use. For example, here's a hypothetical single GraphQL query that can represent both of John's questions to Jane:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight graphql"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="n"&gt;timeLightNeedsToTravel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;toPlanet&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Earth"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="n"&gt;fromTheSun&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;star&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Sun"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="n"&gt;fromTheMoon&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;moon&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Moon"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
John's questions to Jane in GraphQL





&lt;p&gt;This example GraphQL request uses a few of the GraphQL language parts like fields (&lt;code&gt;timeLightNeedsToTravel&lt;/code&gt; and &lt;code&gt;from&lt;/code&gt;), parameters (&lt;code&gt;toPlanet&lt;/code&gt;, &lt;code&gt;star&lt;/code&gt;, and &lt;code&gt;moon&lt;/code&gt;), and aliases (&lt;code&gt;fromTheSun&lt;/code&gt; and &lt;code&gt;fromTheMoon&lt;/code&gt;). These are like the verbs and nouns of the English language. You will learn about all the syntax parts that you can use in GraphQL requests in Chapters 2 and 3.&lt;/p&gt;

&lt;h3&gt;
  
  
  GraphQL is a service
&lt;/h3&gt;

&lt;p&gt;If we teach a client application to speak the GraphQL language, it will be able to communicate any data requirements to a backend data service that also speaks GraphQL. To teach a data service to speak GraphQL, you need to implement a runtime layer and expose that layer to the clients who want to communicate with the service. Think of this layer on the server side as simply a translator of the GraphQL language, or a GraphQL-speaking agent who represents the data service. GraphQL is not a storage engine, so it cannot be a solution on its own. This is why you cannot have a server that speaks just GraphQL and you need to implement a translating runtime layer.&lt;/p&gt;

&lt;p&gt;A GraphQL service can be written in any programming language and it can be conceptually split into two major parts: structure and behavior.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;The structure is defined with a strongly-typed &lt;strong&gt;schema&lt;/strong&gt;. A GraphQL schema is like a catalog of all the operations a GraphQL API can handle. It simply represents the capabilities of an API. GraphQL client applications use the schema to know what questions they can ask the service. The &lt;em&gt;typed&lt;/em&gt; nature of the schema is a core concept in GraphQL. The schema is basically a graph of &lt;strong&gt;fields&lt;/strong&gt; which have &lt;strong&gt;types&lt;/strong&gt; and this graph represents all the possible data objects that can be read (or updated) through the service.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The behavior is naturally implemented with functions that in the GraphQL world are named &lt;strong&gt;resolver functions&lt;/strong&gt; and they represent most of the smart logic behind GraphQL's power and flexibility. Each field in a GraphQL schema is backed by a resolver function. A resolver function defines what data to fetch for its field.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;A resolver function is where we give instructions for the runtime service about how and where to access the raw data. For example, a resolver function might issue a SQL statement to a relational database, read a file's data directly from the operating system, or update some cached data in a document database. A resolver function is directly related to a field in a GraphQL request and it can represent a single primitive value, an object, or a list of values or objects.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;The GraphQL restaurant analogy&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A GraphQL schema is often compared to a restaurant menu. In that analogy, the waiting staff act like instances of the GraphQL API interface. No wonder they use the term "server"! &lt;/p&gt;

&lt;p&gt;Table servers take your orders back to the kitchen, which is the core of the API service. You can compare items in the menu to "fields" in the GraphQL language. If you order a steak, you need to tell your server how you'd like it cooked. That's where you can use field arguments!&lt;/p&gt;

&lt;p&gt;order {&lt;br&gt;
     steak(doneness: MEDIUMWELL)&lt;br&gt;
   }&lt;/p&gt;

&lt;p&gt;Let's say this restaurant is a very busy one. They hired a chef with the sole responsibility of cooking steaks. This chef is the resolver function for the steak field!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;TIP&lt;/strong&gt;: Resolver functions are why GraphQL is often compared to the remote procedure call (RPC) distributed computing concept. GraphQL is essentially a way for clients to invoke remote - resolver - functions.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h4&gt;
  
  
  A schema and resolvers example
&lt;/h4&gt;

&lt;p&gt;To understand how resolvers work, let's look at this simplified &lt;code&gt;employee&lt;/code&gt; query and assume a client sent it to a GraphQL service:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight graphql"&gt;&lt;code&gt;&lt;span class="k"&gt;query&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="n"&gt;employee&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Simplified example query text&lt;/p&gt;

&lt;p&gt;The service can receive and parse any request. It'll then try to validate the request against its schema. The schema has to support a top-level &lt;code&gt;employee&lt;/code&gt; field and that field has to represent an object that has an &lt;code&gt;id&lt;/code&gt; argument, a &lt;code&gt;name&lt;/code&gt; field, and an &lt;code&gt;email&lt;/code&gt; field. Fields and arguments need to have types in GraphQL. The &lt;code&gt;id&lt;/code&gt; argument can be an integer. The &lt;code&gt;name&lt;/code&gt; and &lt;code&gt;email&lt;/code&gt; fields can be strings. The &lt;code&gt;employee&lt;/code&gt; field is a custom type (representing that exact id/name/email structure).&lt;/p&gt;

&lt;p&gt;Just like the client-side query language, the GraphQL community standardized a server-side language dedicated to creating GraphQL schema objects. This language is known as the "Schema Language". It's often abbreviated as SDL (Schema Definition Language) or IDL (Interface Definition Language).&lt;/p&gt;

&lt;p&gt;Here's an example to represent the "Employee" type using GraphQL's schema language:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type Employee(id: Int!) {
  name: String!
  email: String!
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
GraphQL schema language example





&lt;p&gt;This is the custom &lt;code&gt;Employee&lt;/code&gt; type that represents the structure of an employee "model". An object of the employee model can be looked up with an integer &lt;code&gt;id&lt;/code&gt; and it has &lt;code&gt;name&lt;/code&gt; and &lt;code&gt;email&lt;/code&gt; string fields.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;NOTE&lt;/strong&gt;: The exclamation marks after the types mean that they cannot be empty. A client cannot ask for an employee field without specifying an id argument and a valid server response to this field must include a name string and an email string.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;TIP&lt;/strong&gt;: The schema language type definitions are like the CREATE statements that we use to define tables (and other database schema elements). &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Using this type, the GraphQL service can conclude that the &lt;code&gt;employee&lt;/code&gt; GraphQL query is valid because it matches the supported type structure. The next step is to prepare the data it is asking for. To do that, the GraphQL service traverses the tree of fields in that request and invokes the resolver function associated with each field in it. It'll then gather the data returned by these resolver functions and use it to form a single response.&lt;/p&gt;

&lt;p&gt;This example GraphQL service needs to have at least 3 resolver functions: one for the &lt;code&gt;employee&lt;/code&gt; field, one for the &lt;code&gt;name&lt;/code&gt; field, and one for the &lt;code&gt;email&lt;/code&gt; field.&lt;/p&gt;

&lt;p&gt;The employee field's resolver function might, for example, do a query like: &lt;code&gt;select * from employees where id = 42&lt;/code&gt;. This SQL statement returns all columns available on the employees table. Let's say the employees table happens to have the following fields: &lt;code&gt;id&lt;/code&gt;, &lt;code&gt;first_name&lt;/code&gt;, &lt;code&gt;last_name&lt;/code&gt;, &lt;code&gt;email&lt;/code&gt;, &lt;code&gt;birth_date&lt;/code&gt;, &lt;code&gt;hire_date&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;So the employee field's resolver function for employee #42 might return an object like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  id: 42,
  first_name: 'John',
  last_name: 'Doe',
  email: 'john@doe.com'
  birth_date: "01/01/1990",
  hire_date: "01/01/2020"  
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
Response from the database for employee #42





&lt;p&gt;The GraphQL service continues to traverse the fields in the tree one by one invoking the resolver function for each. Each resolver function is passed the result of executing the resolver function of its parent node. So both the &lt;code&gt;name&lt;/code&gt; and &lt;code&gt;email&lt;/code&gt; resolver function receive this object (as their first argument).&lt;/p&gt;

&lt;p&gt;Let's say we have the following (JavaScript) functions representing the server resolver functions for the &lt;code&gt;name&lt;/code&gt; and &lt;code&gt;email&lt;/code&gt; fields:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Resolver functions
const name =&amp;gt; (source) =&amp;gt; `${source.first_name} ${source.last_name}`;
const email =&amp;gt; (source) =&amp;gt; source.email;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;source&lt;/code&gt; object here is the parent node. For top-level fields, the &lt;code&gt;source&lt;/code&gt; object is usually undefined (because there is no parent).&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;TIP: The &lt;code&gt;email&lt;/code&gt; resolver function is known as a "trivial" resolver because the "email" field name matches the "email" property name on the parent source object. Some GraphQL implementations (for example, the JavaScript one) have these trivial resolvers built-in and used as default resolvers if no resolvers were found for a field.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The GraphQL service will use all the responses of these 3 resolver functions to put together the following single response for the &lt;code&gt;employee&lt;/code&gt; GraphQL query:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  data: {
    employee: {
      name: 'John Doe',
      email: 'john@doe.com'
    }
  }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
Example GraphQL response object





&lt;p&gt;We'll start to explore how to write custom resolvers in Chapter 5.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;TIP&lt;/strong&gt;: GraphQL does not require any specific data serialization format but JSON is the most popular one. All the examples in this book will use the JSON format.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Why GraphQL
&lt;/h2&gt;

&lt;p&gt;GraphQL is not the only - or even first - technology to encourage creating efficient data APIs. You can use a JSON-based API with a custom query language or implement the Open Data Protocol (OData) on top of a REST API. Experienced backend developers have been creating efficient technologies for data APIs long before GraphQL. So why exactly do we need a new technology?&lt;/p&gt;

&lt;p&gt;If you ask me to answer the "Why GraphQL" question with just a single word, that word would be: &lt;strong&gt;Standards&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;GraphQL provides standards and structures to implement API features in maintainable and scalable ways while the other alternatives lack such standards.&lt;/p&gt;

&lt;p&gt;GraphQL makes it mandatory for data API servers to publish "documentation" about their capabilities (which is the schema). That schema enables client applications to know everything that's available for them on these servers. The GraphQL standard schema has to be part of every GraphQL API. Clients can ask the service about its schema using the GraphQL language. We'll see examples of that in Chapter 3.&lt;/p&gt;

&lt;p&gt;Other solutions can be made better by adding similar documentations as well. The unique thing about GraphQL here is that the documentation is part of how you create the API service. You cannot have out-of-date documentation. You cannot forget to document a use-case. You cannot offer different ways to use APIs because you have standards to work with. Most importantly, you do not need to maintain the documentation of your API separately from that API. GraphQL documentation is built-in and it's first class!&lt;/p&gt;

&lt;p&gt;The mandatory GraphQL schema represents the possibilities and the limits of what can be answered by the GraphQL service, but there is some flexibility in how to use the schema because we are talking about a graph of nodes here and graphs can be traversed using many paths. This flexibility is one of the great benefits of GraphQL because it allows backend and frontend developers to make progress in their projects without needing to constantly coordinate that progress with each other. It basically decouples clients from servers and allows  both of them to evolve and scale independently. This enables much faster iteration in both frontend and backend products.&lt;/p&gt;

&lt;p&gt;I think this standard schema is among the top benefits of GraphQL but let's also talk about the technological benefits of GraphQL as well.&lt;/p&gt;

&lt;p&gt;One of the biggest technological reasons to consider a GraphQL layer between clients and servers, and perhaps the most popular one, is efficiency. API clients often need to ask the server about multiple resources and the API server usually knows how to answer questions about a single resource. As a result, the client ends up having to communicate with the server multiple times to gather all the data it needs.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--E91dDwjl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://jscomplete.com/images/reads/gia/ch01_fig_04_gqlia.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--E91dDwjl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://jscomplete.com/images/reads/gia/ch01_fig_04_gqlia.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;
A client asking a server about multiple resources



&lt;p&gt;With GraphQL, you can basically shift this multi-request complexity to the backend and have your GraphQL runtime deal with it. The client asks the GraphQL service a single question and gets a single response that has exactly what the client needs. You can customize a REST-based API to provide one exact endpoint per view, but that's not the norm. You will have to implement it without a standard guide.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--rcAXh7dM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://jscomplete.com/images/reads/gia/ch01_fig_05_gqlia.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--rcAXh7dM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://jscomplete.com/images/reads/gia/ch01_fig_05_gqlia.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;
GraphQL shifts multi-request complexities to the backend side



&lt;p&gt;Another big technological benefit about GraphQL is communicating with multiple services. When you have multiple clients requesting data from multiple data storage services (like PostgreSQL, MongoDB, and a REDIS cache), a GraphQL layer in the middle can simplify and standardize this communication. Instead of a client going to the multiple data services directly, you can have that client communicate with the GraphQL service. Then, the GraphQL service will do the communication with the different data services. This is how GraphQL isolates the clients from needing to communicate in multiple languages. A GraphQL service translates a single client's request into multiple requests to multiple services using different languages.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--029QpIhR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://jscomplete.com/images/reads/gia/ch01_fig_06_gqlia.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--029QpIhR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://jscomplete.com/images/reads/gia/ch01_fig_06_gqlia.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;
GraphQL can communicate with different data services



&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;GraphQL is a translator&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Imagine that you have three people who speak three different languages and have different types of knowledge. Then imagine that you have a question that can only be answered by combining the knowledge of all three people together. If you have a translator who speaks all three languages, the task of putting together an answer to your question becomes easy. This is what a GraphQL service can easily do for clients. This point is valid with other data API options, but GraphQL provides standard structures that enable implementing this kind of data need in easier and more maintainable ways.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;One other benefit for GraphQL that I think is often underrated is how it improves the frontend "developer experience". The GraphQL schema gives frontend developers a lot of power and control to explore, construct, validate, test, and accurately perform their data need communication without depending on backend developers. It eliminates the need for the server to hardcode the shape or size of the data and it decouples clients from servers. This means that clients and servers can be developed and maintained separately from each other, which is a big benefit on its own.&lt;/p&gt;

&lt;p&gt;More importantly, with GraphQL, developers express the data requirements of their user interfaces using a declarative language. They express WHAT they need, not HOW to make it available. There is a tight relationship between what data is needed by a UI and the way a developer can express a description of that data need in GraphQL.&lt;/p&gt;

&lt;h3&gt;
  
  
  What about REST APIs?
&lt;/h3&gt;

&lt;p&gt;GraphQL APIs are often compared to REST APIs because the latter has been the most popular choice for data APIs demanded by web and mobile applications. GraphQL provides a more efficient "technology" alternative to REST APIS.  But why do we need an alternative? &lt;em&gt;What is wrong with REST APIs?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The biggest "relevant" problem with REST APIs here is the clients' need to communicate with multiple data API endpoints. REST APIs are an example of servers that require clients to do multiple network round-trips to get data. A REST API is a collection of endpoints where each endpoint represents a resource. So, when a client needs data about multiple resources it needs to perform multiple network requests to that REST API and then put together the data it needs by combining the multiple responses it receives. This is a big problem, especially for mobile applications, because mobile devices usually have processing, memory, and network constraints.&lt;/p&gt;

&lt;p&gt;Furthermore, in a REST API there is no client request language. Clients do not have control over what data the server will return because they do not have a language to communicate their exact needs. More accurately, the language available for clients of a REST API is very limited. For example, the &lt;em&gt;READ&lt;/em&gt; REST API endpoints are either:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;GET /ResourceName&lt;/code&gt; - to get a list of all the records for that resource, or&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;GET /ResourceName/ResourceID&lt;/code&gt; - to get a single record identified by an ID.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In a pure REST API (not a customized one) a client cannot specify which fields to select for a record in that resource. That information is in the REST API service itself and the REST API service will always return all of the fields regardless of which ones the client actually needs. GraphQL's term for this problem is &lt;em&gt;over-fetching&lt;/em&gt; of information that is not needed. It is a waste of network and memory resources for both the client and the server.&lt;/p&gt;

&lt;p&gt;One other big problem with REST APIs is versioning. If you need to support multiple versions that usually means new endpoints. This leads to more problems while using and maintaining these endpoints and it might be the cause of code duplication on the server.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;NOTE&lt;/strong&gt;: The REST APIs problems mentioned here are the ones specific to what GraphQL is trying to solve. They are certainly not all of the problems of REST APIs.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;REST APIs eventually turn into a mix that has regular REST endpoints plus custom ad-hoc endpoints crafted for performance reasons. This is where GraphQL offers a much better alternative.&lt;/p&gt;

&lt;p&gt;It is important to point out here that REST APIs have some advantages over GraphQL APIs. For example, caching a REST API response is a lot easier than caching a GraphQL API response, as you will see in the last section of this chapter. Also, optimizing the code for a REST endpoint is potentially a lot easier than optimizing the code for a generic single endpoint. There is no one magical solution that fixes all problems without introducing new challenges. REST APIs have their place and when used correctly both GraphQL and REST have their great applications. There is also nothing that prohibits the use of both of them together in the same system.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;REST-ish APIs&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Please note that in this book I am talking about pure REST APIs. Some of the problems mentioned here and solved by GraphQL can also be solved by customizing REST APIs. For example, you can modify the REST API to accept an "include" query string that accepts a comma-separated list of fields to return in the response. This will also avoid the over-fetching problem. You can also make a REST API include sub-resources with some query flags. There are tools out there that you can add on top of your REST-based systems and they can enable such customizations or make them easier to implement.&lt;/p&gt;

&lt;p&gt;Such approaches might be okay on a small scale and I have personally used them before with some success. However, when compared to what GraphQL can offer, these approaches require a lot of work and will cause slower iterations in projects. They are also not standardized and will not scale very well for big projects.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  The GraphQL Way
&lt;/h3&gt;

&lt;p&gt;To see the GraphQL way for solving the problems of REST APIs that we talked about, you need to understand the concepts and design decisions behind GraphQL. Here are the major ones:&lt;/p&gt;

&lt;h5&gt;
  
  
  1) The Typed Graph Schema
&lt;/h5&gt;

&lt;p&gt;To create a GraphQL API, you need a typed schema. A GraphQL schema contains fields that have types. Those types can be primitive or custom. Everything in the GraphQL schema requires a type. This static type system is what makes a GraphQL service predictable and discoverable.&lt;/p&gt;

&lt;h5&gt;
  
  
  2) The Declarative Language
&lt;/h5&gt;

&lt;p&gt;GraphQL has a declarative nature for expressing data requirements. It provides clients with a declarative language for them to express their data needs. This declarative nature enables a thinking model in the GraphQL language that is close to the way we think about data requirements in English and it makes working with a GraphQL API a lot easier than the alternatives. &lt;/p&gt;

&lt;h5&gt;
  
  
  3) The Single Endpoint and the Client Language
&lt;/h5&gt;

&lt;p&gt;To solve the multiple round-trip problem, GraphQL makes the responding server work as just one endpoint. Basically, GraphQL takes the custom endpoint idea to an extreme and just makes the whole server a single smart endpoint that can reply to all data requests.&lt;/p&gt;

&lt;p&gt;The other big concept that goes with this single smart endpoint concept is the rich client request language that is needed to work with that single endpoint. Without a client request language, a single endpoint is useless. It needs a language to process a custom request and respond with data for that custom request.&lt;/p&gt;

&lt;p&gt;Having a client request language means that the clients will be in control. They can ask for exactly what they need and the server will reply with exactly what they are asking for. This solves the problem of over-fetching the data that is not needed.&lt;/p&gt;

&lt;p&gt;Furthermore, having clients asking for exactly what they need enables backend developers to have more useful analytics of what data is being used and what parts of the data is in higher demand. This is very useful data. For example, it can be used to scale and optimize the data services based on usage patterns. It can also be used to detect abnormalities and clients' version changes.&lt;/p&gt;

&lt;h5&gt;
  
  
  4) The Simple Versioning
&lt;/h5&gt;

&lt;p&gt;When it comes to versioning, GraphQL has an interesting take. Versioning can be avoided altogether. Basically, you can just add new fields and types without removing the old ones because you have a graph and you can flexibly grow it by adding more nodes. You can leave paths on the graph for old APIs and introduce new ones. The API just grows and no new endpoints are needed. Clients can continue to use older features and they can also incrementally update their code to use new features.&lt;/p&gt;

&lt;p&gt;By using a single evolving version, GraphQL APIs give clients continuous access to new features and encourage cleaner and more maintainable server code.&lt;/p&gt;

&lt;p&gt;This is especially important for mobile clients because you cannot control the version of the API they are using. Once installed, a mobile app might continue to use that same old version of the API for years. On the web, it is easy to control the version of the API because you can just push new code and force all users to use it. For mobile apps, this is a lot harder to do.&lt;/p&gt;

&lt;p&gt;This simple versioning approach has some challenges. Keeping old nodes forever introduces some downsides. More maintenance effort will be needed to make sure old nodes still work as they should. Furthermore, users of the APIs might be confused about which fields are old and which are new. GraphQL offers a way to deprecate (and hide) older nodes so that readers of the schema only see the new ones. Once a field is deprecated, the maintainability problem becomes a question of how long old users will continue to use it. The great thing here is that as a maintainer, you can confidently answer the questions "is a field still being used?" and "how often is a field being used?" thanks to the client query language. The removal of not-used deprecated fields can even be automated.&lt;/p&gt;

&lt;h3&gt;
  
  
  REST APIs and GraphQL APIs in action
&lt;/h3&gt;

&lt;p&gt;Let's go over a one-to-one comparison example between a REST API and a GraphQL API. Imagine that you are building an application to represent the Star Wars films and characters. The first UI you are tackling is a view to show information about a single Star Wars character. This view should display the character's name, birth year, planet name, and the titles of all the films in which they appeared. For example, for Darth Vader, along with his name, the view should display his birth year (41.9BBY), his planet's name (Tatooine), and the titles of the 4 Star Wars film in which he appeared (A New Hope, The Empire Strikes Back, Return of the Jedi, Revenge of the Sith).&lt;/p&gt;

&lt;p&gt;As simple as this view sounds, you are actually dealing with three different resources here: Person, Planet, and Film. The relationship between these resources is simple. We can easily guess the shape of the data needed here. A person object belongs to one planet object and it will have one or more films objects.&lt;/p&gt;

&lt;p&gt;The JSON data for this view could be something like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "data": {
    "person": {
      "name": "Darth Vader",
      "birthYear": "41.9BBY",
      "planet": {
        "name": "Tatooine"
      },
      "films": [
        { "title": "A New Hope" },
        { "title": "The Empire Strikes Back" },
        { "title": "Return of the Jedi" },
        { "title": "Revenge of the Sith" }
      ]
    }
  }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
JSON data example object for a UI component





&lt;p&gt;Assuming that a data service can give us this exact structure, here is one possible way to represent its view with a frontend component library like React.js:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// The Container Component:
&amp;lt;PersonProfile person={data.person}&amp;gt;&amp;lt;/PersonProfile&amp;gt;

// The PersonProfile Component:
Name: {data.person.name}
Birth Year: {data.person.birthYear}
Planet: {data.person.planet.name}
Films: {data.person.films.map(film =&amp;gt; film.title)}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
UI view example in React.js





&lt;p&gt;This is a very simple example. Our experience with Star Wars helped us here to design the shape of the needed data and figure out how to use it in the UI.&lt;/p&gt;

&lt;p&gt;Note one important thing about this UI view. Its relationship with the JSON data object is very clear. The UI view used all the "keys" from the JSON data object. See the values within curly brackets above.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;strong&gt;Now, how can you ask a REST API service for this data?&lt;/strong&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You need a single person's information. Assuming that you know the ID of that person, a REST API is expected to expose that information with an endpoint like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;GET - /people/{id}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This request will give you the name, &lt;code&gt;birthYear&lt;/code&gt;, and other information about the person. A REST API will also give you access to the ID of this person's planet and an array of IDs for all the films this person appeared in.&lt;/p&gt;

&lt;p&gt;The JSON response for this request could be something like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "name": "Darth Vader",
  "birthYear": "41.9BBY",
  "planetId": 1
  "filmIds": [1, 2, 3, 6],
  ... [other information that is not needed for this view]
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Then to read the planet's name, you ask:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;GET - /planets/1
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And to read the films titles, you ask:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;GET - /films/1
GET - /films/2
GET - /films/3
GET - /films/6
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Once you have all six responses from the server, you can combine them to satisfy the data needed by the view.&lt;/p&gt;

&lt;p&gt;Besides the fact that you had to do 6 network round-trips to satisfy a simple data need for a simple UI, the whole approach here is imperative. You gave instructions for how to fetch the data and how to process it to make it ready for the view. For example, you have to deal with the planet's and the films' IDs although the view did not really need them. You had to manually combine multiple data objects although you are implementing a single view that naturally need just a single data object.&lt;/p&gt;

&lt;p&gt;Try asking for this data from a REST API yourself. The Star Wars data has an excellent REST API which is hosted at &lt;a href="https://github.com/graphql/graphql-js"&gt;https://swapi.co&lt;/a&gt; where you can construct the same previous data object. The names of the data elements might be a bit different, but the endpoints structure is the same. You will need to do exactly 6 API calls. Furthermore, you will have to over-fetch information that the view does not need.&lt;/p&gt;

&lt;p&gt;Of course, SWAPI is just one pure implementation of a REST API for this data. There could be better custom implementations that will make this view's data needs easier to fulfill. For example, if the API server implemented nested resources and understood the relationship between a person and a film, you could read the films data (along with the person data) with something like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;GET - /people/{id}/films
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;However, a pure REST API would not have that out-of-the-box. You would need to ask the backend engineers to create this custom endpoint for your view. This is the reality of scaling a REST API. You just add custom endpoints to efficiently satisfy the growing clients' needs. Managing custom endpoints like these is hard.&lt;/p&gt;

&lt;p&gt;For example, if you customized your REST API endpoint to return the films data for a character, that would work great for this view that you are currently implementing. However, in the future, you might need to implement a shorter or longer version of the character's profile information. Maybe you will need to show only one of their films or show the description of each film in addition to the title. Every new requirement will mean a change is needed to customize the endpoint furthermore or even come up with brand new endpoints to optimize the communication needed for the new views. This approach is simply limited.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;strong&gt;Let's now look at the GraphQL approach.&lt;/strong&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A GraphQL server will be just a single smart endpoint. The transport channel would not matter. If you are doing this over HTTP, the HTTP method certainly would not matter either. Let's assume that you have a single GraphQL endpoint exposed over HTTP at &lt;code&gt;/graphql&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Since you want to ask for the data you need in a single network round-trip, you will need a way to express the complete data needs for the server to parse. You do this with a GraphQL query:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;GET or POST - /graphql?query={...}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;A GraphQL query is just a string, but it will have to include all the pieces of the data that you need. This is where the declarative power comes in.&lt;/p&gt;

&lt;p&gt;Let's compare how this simple view's data requirement can be expressed with English and with GraphQL.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# In English:

The view needs:

a person's name,

birth year,

planet's name,

and the titles of all their films.

# In GraphQL:
{
  person(ID: ...) {
    name
    birthYear
    planet {
      name
    }
    films {
      title
    }
  }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
GraphQL vs English





&lt;p&gt;Can you see how close the GraphQL expression is to the English one? It is as close as it can get. Furthermore, compare the GraphQL query with the original JSON data object that we started with.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# GraphQL Query (Question):

{
  person(ID: ...) {
    name
    birthYear
    planet {
      name
    }
    films {
     title
    }
  }
}

# Needed JSON (Answer):
{
  "data": {
    "person": {
      "name": "Darth Vader",
      "birthYear": "41.9BBY",
      "planet": {
        "name": "Tatooine"
      },
      "films": [
        { "title": "A New Hope" },
        { "title": "The Empire Strikes Back" },
        { "title": "Return of the Jedi" },
        { "title": "Revenge of the Sith" }
      ]
     }
  }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
GraphQL vs JSON





&lt;p&gt;The GraphQL query is the exact structure of the JSON data object, except without all the "value" parts. If you think of this in terms of a question-answer relation, the question is the answer statement without the answer part.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If the answer statement is: +&lt;br&gt;
&lt;em&gt;The name of the Star Wars character who has the ID 4 is &lt;em&gt;Darth Vader&lt;/em&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;A good representation of the question is the same statement without the answer part: +&lt;br&gt;
&lt;em&gt;(What is) the name of the Star Wars character who has the ID 4?&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The same relationship applies to a GraphQL query. Take a JSON data object, remove all the "answer" parts (which are the values), and you end up with a GraphQL query suitable to represent a question about that JSON data object.&lt;/p&gt;

&lt;p&gt;Now, compare the GraphQL query with the UI view that used it. Every element of the GraphQL query is used in the UI view and every dynamic part that is used in the UI view appears in the GraphQL query.&lt;/p&gt;

&lt;p&gt;This obvious mapping is one of the greatest powers of GraphQL. The UI view knows the exact data it needs and extracting that requirement from the view code is fairly easy. Coming up with a GraphQL query is simply the task of extracting what is used as variables directly from the UI views. If you think about this in terms of multiple nested UI components, every UI component can ask for the exact part of the data that it needs and the application data needs can be constructed by putting these partial data needs together. GraphQL provides a way for a UI component to define the partial data need via a feature called "Fragments". You will learn about GraphQL fragments in Chapter 3.&lt;/p&gt;

&lt;p&gt;Furthermore, if you invert this mapping model, you will find another powerful concept. If you have a GraphQL query, you know exactly how to use its response in the UI because the query will be the same "structure" as the response. You do not need to inspect the response to know how to use it and you do not need any documentation about the API. It is all built-in.&lt;/p&gt;

&lt;p&gt;Star Wars data has a GraphQL API hosted at &lt;a href="http://graphql.org/swapi-graphql"&gt;graphql.org/swapi-graphql&lt;/a&gt;. You can use the GraphiQL editor available there to test a GraphQL query. We'll talk about the GraphiQL editor in the next Chapter but you can go ahead and try to construct the example data person object there. There are a few minor differences that you will learn about later in the book, but here is the official query you can use against this API to read the data requirement for the same view (with Darth Vader as an example):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight graphql"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="n"&gt;person&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;personID&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="n"&gt;birthYear&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="n"&gt;homeworld&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="n"&gt;filmConnection&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="n"&gt;films&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
GraphQL query for the Star Wars example





&lt;p&gt;Just paste this query in the editor area and hit the run button. This request will give you a response structure very close to what the view used, you expressed this data need in a way that is close to how you would express it in English, and you will be getting all of this data in a single network round-trip.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Is GraphQL a REST killer?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;When I first learned about GraphQL, I tweeted that "REST APIs can REST IN PEACE!". Joking aside however, I don't really think that GraphQL is a REST APIs "killer". I do think however that more people will pick GraphQL over REST for APIs used by web and mobile applications. REST APIs have their place and I don't think that place is for web and mobile applications.&lt;/p&gt;

&lt;p&gt;I like to think that GraphQL will do to REST what JSON did to XML. XML is still pretty heavily used but almost every web-based API I know of today uses the JSON format.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;GraphQL offers many advantages over REST APIs but let's also talk about the challenges GraphQL brings to the table as well.&lt;/p&gt;

&lt;h2&gt;
  
  
  GraphQL Problems
&lt;/h2&gt;

&lt;p&gt;Perfect solutions are fairy tales. With the flexibility GraphQL introduces, a door opens to some clear problems and concerns.&lt;/p&gt;

&lt;h3&gt;
  
  
  Security
&lt;/h3&gt;

&lt;p&gt;One important threat that GraphQL makes easier is resource exhaustion attacks (AKA Denial of Service attacks). A GraphQL server can be attacked with overly complex queries that will consume all the resources of the server. It is very simple to query for deep nested relationships (user -&amp;gt; friends -&amp;gt; friends -&amp;gt; friends …) or use field aliases to ask for the same field many times. Resource exhaustion attacks are not specific to GraphQL, but when working with GraphQL you have to be extra careful about them.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;NOTE&lt;/strong&gt;: This resource-exhaustion problem can also come from non-malignant client applications which have certain bugs or just bad implementations. Remember that a GraphQL client is free to ask for whatever data it requires so it might just ask for too much data at once.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;There are some mitigations you can do here. You can implement cost analysis on the query in advance and enforce some kind of limits on the amount of data one can consume. You can also implement a time-out to kill requests that take too long to resolve. Also, since a GraphQL service is just one layer in any application stack, you can handle the rate limits enforcement at a lower level under GraphQL.&lt;/p&gt;

&lt;p&gt;If the GraphQL API endpoint you are trying to protect is not public and is designed for internal use by your own client applications (web or mobile), you can use a whitelist approach and pre-approve queries that the server can execute. Clients can just ask the servers to execute pre-approved queries using a query unique identifier. While this approach introduces back some dependencies between the servers and the clients, there are some automation strategies that can be used here to mitigate against that. For example, you can give the frontend engineers the freedom to modify the queries and mutations they need to use in development and then automatically replace them with their unique IDs during deployment to production servers. Some client-side GraphQL frameworks are already testing similar concepts.&lt;/p&gt;

&lt;p&gt;Authentication and authorization are other concerns that you need to think about when working with GraphQL. Do you handle them before, after, or during a GraphQL resolve process?&lt;/p&gt;

&lt;p&gt;To answer this question, think of GraphQL as a DSL (Domain Specific Language) on top of your own backend data-fetching logic. It is just one layer that you could put between the clients and your actual data services. Think of authentication and authorization as another layer. GraphQL will not help with the actual implementation of the authentication or authorization logic. It is not meant for that. But if you want to put these layers behind GraphQL, you can use GraphQL to communicate the access tokens between the clients and the enforcing logic. This is very similar to the way authentication and authorization are usually implemented in REST APIs.&lt;/p&gt;

&lt;h3&gt;
  
  
  Caching and Optimizing
&lt;/h3&gt;

&lt;p&gt;One task that GraphQL makes a bit more challenging is client's caching of data. Responses from REST APIs are a lot easier to cache because of their dictionary nature. A certain URL gives a certain data so you can use the URL itself as the cache key.&lt;/p&gt;

&lt;p&gt;With GraphQL, you can adopt a similar basic approach and use the query text as a key to cache its response. But this approach is limited, not very efficient, and can cause problems with data consistency. The results of multiple GraphQL queries can easily overlap and this basic caching approach would not account for the overlap.&lt;/p&gt;

&lt;p&gt;There is a brilliant solution to this problem. A Graph Query means a &lt;em&gt;Graph Cache&lt;/em&gt;. If you normalize a GraphQL query response into a flat collection of records and give each record a global unique ID, you can cache those records instead of caching the full responses.&lt;/p&gt;

&lt;p&gt;This is not a simple process though. There will be records referencing other records and you will be managing a cyclic graph there. Populating and reading the cache will need query traversal. You will probably need to implement a separate layer to handle this cache logic. However, this method will be a lot more efficient than response-based caching.&lt;/p&gt;

&lt;p&gt;One of the other most "famous" problems that you would encounter when working with GraphQL is the problem that is commonly referred to as N+1 SQL queries. GraphQL query fields are designed to be stand-alone functions and resolving those fields with data from a database might result in a new database request per resolved field.&lt;/p&gt;

&lt;p&gt;For a simple REST API endpoint logic, it is easy to analyze, detect, and solve N+1 issues by enhancing the constructed SQL queries. For GraphQL dynamically resolved fields, it is not that simple. &lt;/p&gt;

&lt;p&gt;Luckily, Facebook is pioneering one possible solution to both the caching problem and the data-loading-optimization problem. It's called DataLoader.&lt;/p&gt;

&lt;p&gt;As the name implies, DataLoader is a utility you can use to read data from databases and make it available to GraphQL resolver functions. You can use DataLoader instead of reading the data directly from databases with SQL queries and DataLoader will act as your agent to reduce the SQL queries you send to the database.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--DVyMa0hz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://jscomplete.com/images/reads/gia/ch01_fig_07_gqlia.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--DVyMa0hz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://jscomplete.com/images/reads/gia/ch01_fig_07_gqlia.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;
DataLoader can optimize the requests between GraphQL and databases



&lt;p&gt;DataLoader uses a combination of batching and caching to accomplish that. If the same client request resulted in a need to ask the database about multiple things, DataLoader can be used to consolidate these questions and batch-load their answers from the database. DataLoader will also cache the answers and make them available for subsequent questions about the same resources.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;TIP&lt;/strong&gt;: There are other SQL optimization strategies that you can use. For example, you can construct the optimal join-based SQL queries by analyzing GraphQL requests. If you are using a relational database with native efficient capabilities to join tables of data and re-use previously parsed queries, then a join-based strategy might actually be more efficient in many cases than IDs-based batching. However, IDs-based batching is probably a lot easier to implement.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Learning Curve
&lt;/h3&gt;

&lt;p&gt;Working with GraphQL requires a bigger learning curve than the alternatives. A developer writing a GraphQL-based frontend application will have to learn the syntax of the GraphQL language. A developer implementing a GraphQL backend service will have to learn a lot more than just the language. They'll have to learn the API syntax of a GraphQL implementation. They'll have to learn about schemas and resolvers among many other concepts specific to a GraphQL runtime. &lt;/p&gt;

&lt;p&gt;This is less of an issue in REST APIs for example because they do not have a client language nor do they require any standard implementations. You have the freedom of implementing your REST endpoints however you wish because you don't have to parse, validate, and execute a special language text.&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The best way to represent data in the real world is with a graph data structure. A data model is a graph of related objects. GraphQL embraces this fact.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;A GraphQL system has 2 main components. The query language that can be used by consumers of data APIs to request their exact data needs, and the runtime layer on the backend that publishes a public schema describing the capabilities and requirements of data models. The runtime layer accepts incoming requests on a single endpoint and resolves incoming data requests with predictable data responses. Incoming requests are strings written with the GraphQL query language.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;GraphQL is all about optimizing data communication between a client and a server. GraphQL allows clients to ask for the exact data they need in a declarative way, and it enables servers to aggregate data from multiple data storage resources in a standard way.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;GraphQL has an official specification document that defines standard rules and practices that all implementers of GraphQL runtimes need to adhere to&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;A GraphQL service can be written in any programming language and it can be conceptually split into two major parts: A structure that is defined with a strongly-typed schema representing the capabilities of the API and a behavior that is naturally implemented with functions known as resolvers. A GraphQL schema is a graph of fields which have types. This graph represents all the possible data objects that can be read (or updated) through the GraphQL service. Each field in a GraphQL schema is backed by a resolver function&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The difference between GraphQL and its previous alternatives is in the fact that it provides standards and structures to implement API features in maintainable and scalable ways. The other alternatives lack such standards. GraphQL also solves many technical challenges like needing to do multiple network round-trips and having to deal with multiple data responses on the client&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;GraphQL comes with some many challenges especially in the areas of security and optimization. Because of the flexibility it provides, securing a GraphQL API requires thinking about more vulnerabilities. Caching a flexible GraphQL API is also a lot harder than caching fixed API endpoints (as in REST APIs). The GraphQL learning curve is also bigger than many of its alternatives.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;strong&gt;Thanks for reading! The book is available at &lt;a href="http://bit.ly/graphql-in-action"&gt;bit.ly/graphql-in-action&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>graphql</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>webdev</category>
    </item>
    <item>
      <title>The API I wish JavaScript GraphQL implementations supported</title>
      <dc:creator>Samer Buna</dc:creator>
      <pubDate>Mon, 10 Jun 2019 23:25:56 +0000</pubDate>
      <link>https://forem.com/pluralsight/the-api-i-wish-javascript-graphql-implementations-supported-3k1n</link>
      <guid>https://forem.com/pluralsight/the-api-i-wish-javascript-graphql-implementations-supported-3k1n</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--dNo2dAjx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2068/1%2Au_vXPo0vhgV6Hkp7EbT45g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--dNo2dAjx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2068/1%2Au_vXPo0vhgV6Hkp7EbT45g.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The GraphQL &lt;a href="https://graphql.org/learn/schema/"&gt;schema language&lt;/a&gt; is great! It is certainly the best way to communicate anything about a GraphQL service. No wonder all documentations now use it!&lt;/p&gt;

&lt;h2&gt;
  
  
  The Schema Language
&lt;/h2&gt;

&lt;p&gt;Imagine that you’re building a blog app (with GraphQL) that has “Articles” and “Comments” . You can start thinking about its API schema by basing it on what you plan for its UI. For example, the main page will probably have a list of articles and an item on that list might display a title, subtitle, author’s name, publishing date, length (in reading minutes), and a featured image. A simplified version of Medium itself if you may:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--_70BfT0G--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/3084/1%2AcD5wtJ1V33DXOsYh1pM6qg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--_70BfT0G--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/3084/1%2AcD5wtJ1V33DXOsYh1pM6qg.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can use the schema-language to plan what you need so far for that main page. A basic schema might look like:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type Query {
  articleList: [Article!]!
}

type Article {
  id: ID!
  title: String!
  subTitle: String
  featuredImageUrl: String
  readingMinutes: Int!
  publishedAt: String!

author: Author!
}

type Author {
  name: String!
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;When a user navigates to an article, they’ll see the details of that article. We’ll need the API to support a way to retrieve an Article object by its &lt;code&gt;id&lt;/code&gt;. Let’s say an article can also have rich UI elements like headers and code snippets. We would need to support a rich-text formatting language like Markdown. We’ll make the API return an article’s content in either Markdown or HTML through a field argument (&lt;code&gt;format&lt;/code&gt;: HTML). Let’s also plan to display a likes counter in that view.&lt;/p&gt;

&lt;p&gt;Put all these ideas on paper! The schema language is the most concise structured way to describe them:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type Query {
  # ...
  article(id: String!): Article!
}

enum ContentFormat {
  HTML
  MARKDOWN
}

type Article {
  # ...
  content(format: ContentFormat): String!
  likes: Int!
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The one article’s UI view will also display the list of comments available on an article. Let’s keep the comment UI view simple and plan it to have a text content and an author name fields:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type Article {
  # ...
  commentList: [Comment!]!
}

type Comment {
  id: ID!
  content: String!
  author: Author!
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;&lt;em&gt;Let’s focus on just these features. This is a good starting point that’s non-trivial. To offer these capabilities we’ll need to implement custom resolving logic for computed fields like &lt;code&gt;content(format: HTML)&lt;/code&gt; and &lt;code&gt;readingMinutes&lt;/code&gt;. We’ll also need to implement 1–1 and 1-many db relationships.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Did you notice how I came up with the whole schema description so far just by thinking in terms of the UI. How cool is that? You can give this simple schema language text to the front-end developers on your team and they can start building the front-end app right away! They don’t need to wait for your server implementation. They can even use some of the great tools out there to have a mock GraphQL server that resolves these types with random test data.&lt;/p&gt;

&lt;blockquote&gt;
&lt;h1&gt;
  
  
  The schema is often compared to a contract. You always start with a contract.
&lt;/h1&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Building a GraphQL Schema
&lt;/h2&gt;

&lt;p&gt;When you’re ready to start implementing your GraphQL service, you have 2 main options (in JavaScript) today:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;You can “build” a non-executable schema using the full schema language text that we have and then attach a set of resolver functions to make that schema executable. You can do that with &lt;a href="https://graphql.org/graphql-js/"&gt;GraphQL.js&lt;/a&gt; itself or with &lt;a href="https://www.apollographql.com/docs/apollo-server/"&gt;Apollo Server&lt;/a&gt;. Both support this method which is commonly known as “schema-first” or “SDL-first”. I’ll refer to it here as the “&lt;strong&gt;full-schema-string method&lt;/strong&gt;”.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;You can use JavaScript objects instantiated from the various constructor classes that are available in the GraphQL.js API (like &lt;code&gt;GraphQLSchema&lt;/code&gt;, &lt;code&gt;GraphQLObjectType&lt;/code&gt;, &lt;code&gt;GraphQLUnionType&lt;/code&gt;, and many others). In this approach, you don’t use the schema-language text at all. You just create objects. This method is commonly known as “code-first” or “resolvers-first” but I don’t think these names fairly represent it. I’ll refer to it here as the “&lt;strong&gt;object-based method&lt;/strong&gt;”.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Both approaches have advantages and disadvantages.&lt;/p&gt;

&lt;p&gt;The schema language is a great programming-language-agnostic way to describe a GraphQL schema. It’s a human-readable format that’s easy to work with. The frontend people on your team will absolutely love it. It enables them to participate in the design of the API and, more importantly, start using a mocked version of it right away. The schema language text can serve as an early version of the API documentation.&lt;/p&gt;

&lt;p&gt;However, completely relying on the full schema language text to create a GraphQL schema has a few drawbacks. You’ll have to put in some effort to make the code modularized and clear and you have to rely on coding patterns and tools to keep the schema-language text consistent with the tree of resolvers (AKA resolvers map). These are solvable problems.&lt;/p&gt;

&lt;p&gt;The biggest problem I see with the full-schema-string method is that you lose some flexibility in your code. You don’t have objects associated with types. You just have strings! And although these strings make your types more readable, in many cases you’ll need the flexibility over the readability.&lt;/p&gt;

&lt;p&gt;The object-based method is flexible and easier to extend and manage. It does not suffer from any of the mentioned problems. You &lt;em&gt;have&lt;/em&gt; to be modular with it because your schema is a bunch of objects. You also don’t need to merge modules together because these objects are designed and expected to work as a tree.&lt;/p&gt;

&lt;p&gt;The only problem I see with the object-based method is that you have to deal with a lot more code around what’s important to manage in your modules (types and resolvers). A lot of developers see that as “noise” and you can’t blame them. We’ll work through an example to see that.&lt;/p&gt;

&lt;p&gt;If you’re creating a small-scope and well-defined GraphQL service, using the full-schema-string method is probably okay. However, in bigger and more agile projects I think the more flexible and more powerful object-based method is the way to go.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;You should still leverage the schema-language text even if you’re using the object-based method. At &lt;a href="https://jscomplete.com"&gt;jsComplete&lt;/a&gt;, we use the object-based method but every time the schema is built we use the &lt;code&gt;graphql.printSchema&lt;/code&gt; function to write the complete schema to a file. We commit and track that file in the Git repository of the project and that proved to be a very helpful practice!&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;To compare the 2 methods, I’ve implemented an executable schema for the blog example we started with using both of them. I’ve omitted some code for brevity but kept what matters for the comparison.&lt;/p&gt;

&lt;h3&gt;
  
  
  The full-schema-string method
&lt;/h3&gt;

&lt;p&gt;We start with the schema-language text which defines 3 main custom types (&lt;code&gt;Article&lt;/code&gt;, &lt;code&gt;Comment&lt;/code&gt;, and &lt;code&gt;Author&lt;/code&gt;). The fields under the main Query type are &lt;code&gt;article&lt;/code&gt; and &lt;code&gt;articleList&lt;/code&gt; which will directly resolve objects from the database. However, since the GraphQL schema we planned has custom features around an article object and since we have relations that we need to resolve as well we’ll need to have custom resolvers for the 3 GraphQL types.&lt;/p&gt;

&lt;p&gt;Here are a few screenshots for the code I wrote to represent the full-schema-string method. I’ve used Apollo Server here but this is also possible with vanilla GraphQL.js (and a bit more code).&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Please note that this is just ONE way of implementing the full-schema-string method for this service. There are countless other ways. I am just presenting the simplest modular way here to help us understand the true advantages and disadvantages.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--mQMwDzOq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/3616/1%2AIAEuUxNsfaS5uTRw42f_4Q.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--mQMwDzOq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/3616/1%2AIAEuUxNsfaS5uTRw42f_4Q.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is nice! We can see the types in the schema in one place. It’s clear where the schema starts. We’re able to modularize the code by type/feature.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--wsO75Zbk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/4152/1%2Aah_kyrmC9uh-b2dE_4rH5g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--wsO75Zbk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/4152/1%2Aah_kyrmC9uh-b2dE_4rH5g.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This again is really great. Resolvers are co-located with the types they implement. There is no noise. This file beautifully contains what matters in a very readable format. I love it!&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The modularity here is only possible with Apollo Server. If we’re to do this with vanilla GraphQL.js we will have to monkey with data objects to make them suitable to be a “resolvers tree”. The mixing between the data structures and the resolvers graph is not ideal.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;So what’s the downside here?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you use this method then all your types have to be written in that certain way that relies on the schema language text. You have less flexibility. You can’t use constructors to create &lt;strong&gt;some&lt;/strong&gt; types when you need to. You’re locked down to this string-based approach.&lt;/p&gt;

&lt;p&gt;If you’re okay with that then ignore the rest of this article. Just use this method. It is so much cleaner than the alternative.&lt;/p&gt;

&lt;h3&gt;
  
  
  The object-based method
&lt;/h3&gt;

&lt;p&gt;Let’s now look at the object-based approach. Here’s the starting point of an executable schema built using that method:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--NJufs5Wr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/3848/1%2AQOG32JcLvMl1k160GAroyw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--NJufs5Wr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/3848/1%2AQOG32JcLvMl1k160GAroyw.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We don’t need a separate &lt;code&gt;resolvers&lt;/code&gt; object. Resolvers are part of the schema object itself. That makes them easier to maintain. This code is also easier to programmatically extend and analyze!&lt;/p&gt;

&lt;p&gt;It’s also so much more code that’s harder to read and reason about! Wait until you see the rest of the code. I couldn’t take the &lt;code&gt;Article&lt;/code&gt; type screenshot on the laptop screen. I had to use a bigger screen.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--rEnLT7-f--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/4076/1%2AVA13wQ8zkofwUMTGGzpkNg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--rEnLT7-f--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/4076/1%2AVA13wQ8zkofwUMTGGzpkNg.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;No wonder the full-schema-string method is popular! There is certainly a lot of “noise” to deal with here. Types are not clear at first glance. Custom resolvers are mixed in one big configuration object.&lt;/p&gt;

&lt;p&gt;My favorite part is when you need to create a non-null list of non-null items like &lt;code&gt;[Article!]!&lt;/code&gt;. Did you see what I had to write?&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;new GraphQLNonNull(new GraphQLList(new GraphQLNonNull(Article))),
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;However, while this is indeed a lot more code that’s harder to understand, it is still a better option than having one big string (or multiple strings combined into one) and one big root resolvers object (or multiple resolvers objects combined into one). It’s better than having all the dependencies of your app managed in one single entry point.&lt;/p&gt;

&lt;p&gt;There is a lot of power in modularizing your code using objects (that may depend on each other). It’s cleaner that way and it also makes writing tests and validations easier. You get more helpful error messages when you debug problems. Modern editors can provide more helpful hints in general. Most importantly, you have a lot more flexibility to do anything with these objects. The GraphQL.js constructors API itself also uses JavaScript objects. There is so much you can do with them.&lt;/p&gt;

&lt;p&gt;But the noise is real too.&lt;/p&gt;

&lt;h3&gt;
  
  
  The object-based method without the noise
&lt;/h3&gt;

&lt;p&gt;I am sticking with the object-based method but I sure wish the JavaScript GraphQL implementations had a better API that can give us some of the power of the full-schema-string method.&lt;/p&gt;

&lt;p&gt;Wouldn’t be nice if we can write the &lt;code&gt;Article&lt;/code&gt; type logic exactly as we did in the full-schema-string method but in a way that generates the flexible &lt;code&gt;GraphQLObjectType&lt;/code&gt; that we can plug into an object-based schema?&lt;/p&gt;

&lt;p&gt;Something like:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--0VPjzrOc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/3988/1%2AeYYilCRjfJS6WW0kKJbTuQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--0VPjzrOc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/3988/1%2AeYYilCRjfJS6WW0kKJbTuQ.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Wouldn’t that be ideal? &lt;strong&gt;We get the benefits of the full-schema-string method for this type but with no lockdown!&lt;/strong&gt; Other types in the system can be maintained differently. Maybe other types will be dynamically constructed using a different maker logic!&lt;/p&gt;

&lt;p&gt;All we need to make this happen is a magical &lt;strong&gt;typeMakerMethod&lt;/strong&gt; to take the parts that matter and transform them into the complete &lt;code&gt;GraphQLObjectType&lt;/code&gt; for &lt;code&gt;Article&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;typeMakerMethod&lt;/code&gt; will need to parse a string into an AST, use that to build a GraphQLObjectType, then merge the set of custom resolver functions with the &lt;code&gt;fields&lt;/code&gt; configuration that’ll be parsed from the &lt;code&gt;typeDef&lt;/code&gt; string.&lt;/p&gt;

&lt;p&gt;I like a challenge so I dug a little bit deeper to see how hard would it be to implement the &lt;code&gt;typeMakerMethod&lt;/code&gt;. I knew I couldn’t use the &lt;code&gt;graphql.buildSchema&lt;/code&gt; function because it only parses one full schema string to make a non executable schema object. I needed a lower-level part that parses a string that has exactly ONE type and then attaches custom resolvers to it. So I started reading the source code of GraphQL.js to look for clues. A few cups of coffee later, I found some answers (in 2 places):&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--LTy8tdy9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2456/1%2Al_UCRqQUpdExyxOHW_xs8g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--LTy8tdy9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2456/1%2Al_UCRqQUpdExyxOHW_xs8g.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That’s the core method used in &lt;code&gt;buildSchema&lt;/code&gt; to construct ONE type from a type definition node (which we can easily get by parsing the &lt;code&gt;typeDef&lt;/code&gt; string).&lt;/p&gt;

&lt;p&gt;And:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--CistAHJr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2728/1%2A9875BHFxpFSGmY9NXWZT3w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--CistAHJr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2728/1%2A9875BHFxpFSGmY9NXWZT3w.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That’s how easy it is to extend an object type and attach any logic needed in &lt;code&gt;fields&lt;/code&gt; and &lt;code&gt;interfaces&lt;/code&gt;!&lt;/p&gt;

&lt;p&gt;All I needed to do is put a few pieces together and the dream can be true.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/jscomplete/graphql-makers#readme"&gt;&lt;strong&gt;I did&lt;/strong&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Ladies and gentlemen. I present to you the magical “typeMakerMethod” (which I named &lt;code&gt;objectType&lt;/code&gt;):&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--t9mtdIRK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/3960/1%2ALIFeRi2IS2EWXLSX-cq2Xw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--t9mtdIRK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/3960/1%2ALIFeRi2IS2EWXLSX-cq2Xw.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That’s it (in its most basic form)! This will take a &lt;code&gt;typeDef&lt;/code&gt; string that defines a single GraphQL type, an object of resolvers and a map of dependencies (for that type), and it’ll return a &lt;code&gt;GraphQLObjectType&lt;/code&gt; ready to be plugged into your object-based schema as if it was defined normally with the object constructor.&lt;/p&gt;

&lt;p&gt;Now you can use the object-based method but you have the option to define SOME types using an approach similar to the full-schema-string method. You have the power.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What do you think of this approach? I’d love to hear your feedback!&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Please note that the &lt;code&gt;objectType&lt;/code&gt; code above is just the &lt;strong&gt;basic use case&lt;/strong&gt;. There are many other use cases that require further code. For example, if the types have circular dependencies (&lt;code&gt;article&lt;/code&gt; → &lt;code&gt;author&lt;/code&gt; → &lt;code&gt;article&lt;/code&gt;) then this version of &lt;code&gt;objectType&lt;/code&gt; will not work. We can delay the loading of the circular dependencies until we’re in the &lt;code&gt;fields&lt;/code&gt; thunk (which is the current approach to solve this problem in the object-based method). We can also use the "extend" syntax to design the schema in a way that avoids circular dependencies in the first place. I’ve skipped this part to keep the example simple.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;If you’d like to give it a spin I published a more polished version of &lt;code&gt;objectType&lt;/code&gt; and a few other maker functions like it under the &lt;a href="https://www.npmjs.com/package/graphql-makers"&gt;&lt;strong&gt;graphql-makers&lt;/strong&gt;&lt;/a&gt; npm package.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;em&gt;Originally published at &lt;a href="https://jscomplete.com/learn/19xfr-the-api-I-wish-graphql-implementations-supported"&gt;https://jscomplete.com&lt;/a&gt; on June 9, 2019.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>graphql</category>
      <category>javascript</category>
      <category>webdev</category>
      <category>showdev</category>
    </item>
    <item>
      <title>Which JavaScript frameworks should you learn in 2018?</title>
      <dc:creator>Samer Buna</dc:creator>
      <pubDate>Wed, 06 Jun 2018 16:23:41 +0000</pubDate>
      <link>https://forem.com/samerbuna/which-javascript-frameworks-should-you-learn-in-2018-e5e</link>
      <guid>https://forem.com/samerbuna/which-javascript-frameworks-should-you-learn-in-2018-e5e</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--PrxsE7Ph--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1600/1%2ApPJq-yvXA13X4g87Lc4NJQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--PrxsE7Ph--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1600/1%2ApPJq-yvXA13X4g87Lc4NJQ.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let me first define what I think a coding framework is because the word is debatable. I think a framework is a library that defines structures about every aspect or layer that can make the task of creating applications easier. By layers, I mean things like databases, models, controllers, views, presenters, networks, etc..&lt;/p&gt;

&lt;p&gt;Frameworks try to solve most of the big and known problems that are usually encountered by their applications. They have built-in design decisions that you do not need to worry about. They also have carefully-crafted guidelines.&lt;/p&gt;

&lt;p&gt;Good frameworks also have smart defaults and follow the convention over configuration concept. The leader of this is the &lt;strong&gt;Ruby on Rails&lt;/strong&gt; framework, which is one of my favorites. In JavaScript land, the leading JavaScript framework out there is &lt;strong&gt;Angular&lt;/strong&gt;. (It's not &lt;strong&gt;React.js&lt;/strong&gt; because &lt;a href="https://dev.to/samerbuna/yes-react-is-taking-over-front-end-development-the-question-is-why-4mad"&gt;I don't think of React.js as a framework&lt;/a&gt;)&lt;/p&gt;




&lt;p&gt;Now that we cleared this point, let me answer the question in the title: &lt;strong&gt;Which JavaScript frameworks should you learn in 2018?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;My answer: &lt;strong&gt;None.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let me explain why.&lt;/p&gt;

&lt;p&gt;Unless you are in a job that requires you to use a framework, don’t learn frameworks just for the sake of learning. I am talking about learning how to &lt;strong&gt;use&lt;/strong&gt; a framework. This is a knowledge that will expire fast.&lt;/p&gt;

&lt;p&gt;On the other hand, you should learn &lt;strong&gt;how frameworks work&lt;/strong&gt;. That is very valuable knowledge. If your motivation to learn a framework is to land a good job, knowing how frameworks work is far more impressive than knowing how to use them.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Learn how to use a framework only when you actually need it.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I think there are far more important skills to learn to become a more in-demand developer:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Learn everything you can about the JavaScript language itself, its bad parts and good parts, and all the modern features it recently gained. Learn how to build and use the various data structures in JavaScript.&lt;/li&gt;
&lt;li&gt;Learn the JavaScript runtimes like Node and Browsers and learn how they are single-threaded. Learn their APIs and limitations. Learn about the event loop. Learn about the VM’s call stacks. Get comfortable with browsers’ DevTools.&lt;/li&gt;
&lt;li&gt;Learn how to share code between clients and servers and preload initial data. Learn how to minimize JavaScript loading and parsing in browsers. Learn how to load JavaScript on demand.&lt;/li&gt;
&lt;li&gt;Learn the merits of functional programming and use its concepts where you can. Learn how to be declarative instead of imperative when you can.&lt;/li&gt;
&lt;li&gt;Learn about small JavaScript libraries which do one thing and do it well. Pick libraries with the smallest API and don’t focus on the APIs but rather on what these libraries enable you to do.&lt;/li&gt;
&lt;li&gt;Learn how to build scalable data APIs (take a look at GraphQL).&lt;/li&gt;
&lt;li&gt;Learn the power of CSS and how to use it to minimize your applications JavaScript code. Learn the new Flexbox and Grid layouts. Learn about UI-responsive design.&lt;/li&gt;
&lt;li&gt;Learn how to add static types to JavaScript with TypeScript (or Flow) and learn where you should focus on types vs testing.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Thanks for reading.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;This article was originally published &lt;a href="https://medium.com/@samerbuna/which-javascript-frameworks-should-you-learn-in-2018-ecea9a27617d"&gt;here&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;




&lt;p&gt;When you're ready to learn React or Node, checkout my books ;)&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://jscomplete.com/learn-react-js-by-building-games"&gt;Learn React.js by Building Games&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://jscomplete.com/learn-node-js-beyond-the-basics"&gt;Node.js Beyond the Basics&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I create &lt;strong&gt;online courses&lt;/strong&gt; for &lt;a href="https://app.pluralsight.com/profile/author/samer-buna"&gt;Pluralsight&lt;/a&gt;, &lt;a href="https://www.lynda.com/Samer-Buna/7060467-1.html"&gt;Lynda&lt;/a&gt; and &lt;a href="https://jscomplete.com/samer-buna"&gt;many more&lt;/a&gt;. I also do &lt;strong&gt;online and onsite training&lt;/strong&gt; for teams covering beginner to advanced levels in JavaScript, Node.js, React.js, and GraphQL. Email &lt;a href="//mailto:training@agilelabs.com"&gt;training@agilelabs.com&lt;/a&gt; if you want to book a session for your team.&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>webdev</category>
      <category>programming</category>
    </item>
    <item>
      <title>Yes, React is taking over front-end development. The question is why.</title>
      <dc:creator>Samer Buna</dc:creator>
      <pubDate>Wed, 06 Jun 2018 16:18:40 +0000</pubDate>
      <link>https://forem.com/samerbuna/yes-react-is-taking-over-front-end-development-the-question-is-why-4mad</link>
      <guid>https://forem.com/samerbuna/yes-react-is-taking-over-front-end-development-the-question-is-why-4mad</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--E_Z73aId--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2A3IUcek7o2S5aJnFAgtP5Gg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--E_Z73aId--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2A3IUcek7o2S5aJnFAgtP5Gg.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;The focus of this article is about React’s popularity and not how it compares to other frameworks or libraries&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Here are a few reasons why React has become so popular so quickly:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Working with the DOM API is (mostly) hard. React basically gives developers the ability to work with a virtual browser that is friendlier than the real browser.&lt;/li&gt;
&lt;li&gt;React is just JavaScript. There is a very small API to learn, and after that your JavaScript skills are what make you a better React developer. There are no barriers to entry.&lt;/li&gt;
&lt;li&gt;Learning React pays off big-time for iOS and Android mobile applications as well. React Native allows you to use your React skills to build native mobile applications. You can even share some logic between your web, iOS, and Android applications.&lt;/li&gt;
&lt;li&gt;The React team at Facebook tests all improvements and new features that get introduced to React right there on facebook.com, which increases the trust in the library among the community. It's rare to see big and serious bugs in React releases because they only get released after thorough production testing at Facebook.&lt;/li&gt;
&lt;li&gt;Most importantly, React enables developers to declaratively describe their User Interfaces and model the state of those interfaces. This means instead of coming up with steps to describe transactions on interfaces, developers just describe the interfaces in terms of a final state (like a function). When transactions happen to that state, React takes care of updating the User Interfaces based on that. &lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;Let's get into more details. One famous advantage about React is its &lt;strong&gt;Virtual DOM&lt;/strong&gt; (or reconciliation algorithm). We’ll work through an example to show the actual practical value of having such an algorithm at your command.&lt;/p&gt;

&lt;p&gt;React’s official definition states that it’s a &lt;em&gt;JavaScript library for building User Interfaces&lt;/em&gt;. It’s important to understand the two different parts of this definition:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;React is a &lt;em&gt;JavaScript library&lt;/em&gt;. It’s not a framework. It’s not a complete solution and we’ll often need to use more libraries with React to form any solution. React does not assume anything about the other parts in any full solution. It focuses on just one thing, and on doing that thing very well.&lt;/li&gt;
&lt;li&gt;The thing that React does really well is the second part of the definition: &lt;em&gt;building User Interfaces&lt;/em&gt;. A User Interface is anything we put in front of users to have them interact with a machine. User Interfaces are everywhere, from the simple buttons on a microwave to the dashboard of a space shuttle. If the device we’re trying to interface can understand JavaScript, we can use React to describe a User Interface for it.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Since Web browsers understand JavaScript, we can use React to describe Web User Interfaces. I like to use the word &lt;em&gt;describe&lt;/em&gt; here because that’s what &lt;em&gt;we&lt;/em&gt; basically do with React, we just tell it what we want and React will build the actual User Interfaces, on our behalf, in the Web browser. Without React or similar libraries, we would need to manually build User Interfaces with native Web APIs and JavaScript.&lt;/p&gt;

&lt;p&gt;When you hear the statement that “React is declarative,” this is exactly what it means, we describe User Interfaces with React and tell it what we want (not how to do it). React will take care of the “how” and translate our declarative descriptions (which we write in the React language) to actual User Interfaces in the browser. React shares this simple declarative power with HTML itself, but with React, we get to be declarative for HTML interfaces that represent dynamic data, not just static data.&lt;/p&gt;

&lt;p&gt;React has three main design concepts that drive its popularity:&lt;/p&gt;

&lt;h4&gt;
  
  
  1 — The use of reusable, composable, and stateful components
&lt;/h4&gt;

&lt;p&gt;In React, we describe User Interfaces using components. You can think of components as simple functions (in any programming language). We call functions with some input and they give us some output. We can reuse functions as needed and compose bigger functions from smaller ones.&lt;/p&gt;

&lt;p&gt;Components are exactly the same; we call their input “properties” and “state”, and a component output is a description of a User Interface (which is similar to HTML for browsers). We can reuse a single component in multiple User Interfaces, and components can contain other components.&lt;/p&gt;

&lt;p&gt;Unlike pure functions however, a full React component can have a private state to hold data that may change over time.&lt;/p&gt;

&lt;h4&gt;
  
  
  2 — The nature of reactive updates
&lt;/h4&gt;

&lt;p&gt;React’s name is the simple explanation for this concept. When the state of a component (the input) changes, the User Interface it represents (the output) changes as well. This change in the description of the User Interface has to be reflected in the device we’re working with.&lt;/p&gt;

&lt;p&gt;In a browser, we need to regenerate the HTML views in the Document Object Model (DOM). With React, we do not need to worry about &lt;em&gt;how&lt;/em&gt; to reflect these changes, or even manage &lt;em&gt;when&lt;/em&gt; to take changes to the browser; React will simply &lt;em&gt;react&lt;/em&gt; to the state changes and automatically update the DOM when needed.&lt;/p&gt;

&lt;h4&gt;
  
  
  3 — The virtual representation of views in memory
&lt;/h4&gt;

&lt;p&gt;With React, we write HTML using JavaScript. We rely on the power of JavaScript to generate HTML that depends on some data, rather than enhancing HTML to make it work with that data. Enhancing HTML is what other JavaScript frameworks usually do. For example, Angular extends HTML with features like loops, conditionals, and others.&lt;/p&gt;

&lt;p&gt;When we receive just the data from the server (in the background, with AJAX), we need something more than HTML to work with that data. It’s either using an enhanced HTML, or using the power of JavaScript itself to generate the HTML. Both approaches have advantages and disadvantages. React embraces the latter one, with the argument that the advantages are stronger than the disadvantages.&lt;/p&gt;

&lt;p&gt;In fact, there is one major advantage that can make the case for this approach by itself; using JavaScript to render HTML makes it easy for React to keep a virtual representation of HTML in memory (which is commonly known as &lt;em&gt;The Virtual DOM&lt;/em&gt;). React uses the Virtual DOM to render an HTML tree virtually first, and then, every time a state changes and we get a new HTML tree that needs to be taken to the browser’s DOM, instead of writing the whole new tree React will only write the difference between the new tree and the previous tree (since React has both trees in memory). This process is known as &lt;em&gt;Tree Reconciliation&lt;/em&gt;, and I think, it is the best thing that has happened in Web Development since AJAX!&lt;/p&gt;

&lt;p&gt;In the following example, we’ll focus on this last concept and see a simple practical example of the tree reconciliation process and the big difference it makes. We’ll write the same HTML example twice, first using native Web APIs and vanilla JavaScript, and then we’ll see how to describe the same HTML tree with React.&lt;/p&gt;

&lt;p&gt;To purely focus on this last concept, we will not be using components, and we will mock a state change operation using a JavaScript timer. We are also not going to use JSX, although using JSX will make for a much simpler code. I use JSX all the time when I write React, but working with React API directly in this example will hopefully make you understand this concept much better.&lt;/p&gt;

&lt;h4&gt;
  
  
  React’s reconciliation algorithm example
&lt;/h4&gt;

&lt;p&gt;To follow along with this example, you need a browser and a code editor. You can actually use an online coding playground, but I’ll use local files and test them directly in a browser (we don’t need a web server):&lt;/p&gt;

&lt;p&gt;We’ll start this example from scratch. Create a new directory, and launch your favorite editor there:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mkdir react-democd react-demoatom .
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Create an &lt;code&gt;index.html&lt;/code&gt; file in that directory, and put a standard HTML template in there. Include in that template a &lt;code&gt;script.js&lt;/code&gt; file and put a &lt;code&gt;console.log&lt;/code&gt; statement in that script to test that the include works:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;!DOCTYPE html&amp;gt;
&amp;lt;html&amp;gt;
  &amp;lt;head&amp;gt;
    &amp;lt;meta charset="utf-8"&amp;gt;
    &amp;lt;title&amp;gt;React Demo&amp;lt;/title&amp;gt;
  &amp;lt;/head&amp;gt;
  &amp;lt;body&amp;gt;
    &amp;lt;script src="script.js"&amp;gt;&amp;lt;/script&amp;gt; &amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Open the &lt;code&gt;index.html&lt;/code&gt; file in your browser and make sure you can see the empty template without problems, and that you can see in the Console dev-tools tab the &lt;code&gt;console.log&lt;/code&gt; test message that you put in &lt;code&gt;script.js&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;open index.html # On Macexplorer index.html # On Windows
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, let’s bring in the React library itself, which we can include from the &lt;a href="https://reactjs.org/docs/cdn-links.html"&gt;Reactjs website&lt;/a&gt;. Copy both the &lt;code&gt;react&lt;/code&gt; and &lt;code&gt;react-dom&lt;/code&gt; scripts, and include them in &lt;code&gt;index.html&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"&amp;gt;&amp;lt;/script&amp;gt;
&amp;lt;script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"&amp;gt;&amp;lt;/script&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We’re including two different scripts here for an important reason: The &lt;code&gt;React&lt;/code&gt; library itself can be used without a browser. To use React with a browser, we need the &lt;code&gt;ReactDOM&lt;/code&gt; library.&lt;/p&gt;

&lt;p&gt;When we refresh the browser now, we should see both &lt;code&gt;React&lt;/code&gt; and &lt;code&gt;ReactDOM&lt;/code&gt; available on the global scope:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--R-PNVJSE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1600/1%2Ag5-fvPYO0bTelGckK9RWzA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--R-PNVJSE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1600/1%2Ag5-fvPYO0bTelGckK9RWzA.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;With this simple setup, we can now access both &lt;code&gt;React&lt;/code&gt; and &lt;code&gt;ReactDOM&lt;/code&gt; APIs, and of course, we also have access to the native Web APIs and JavaScript which we are going to use first.&lt;/p&gt;

&lt;p&gt;To insert HTML dynamically in the browser we can simply use pure JavaScript and the DOM Web API itself. Let’s create a &lt;code&gt;div&lt;/code&gt; element to host our JavaScript HTML content and give it the id &lt;code&gt;"js"&lt;/code&gt;. In the body element of &lt;code&gt;index.html&lt;/code&gt;, right before the &lt;code&gt;script&lt;/code&gt; tag, add:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div id="js"&amp;gt;&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now in &lt;code&gt;script.js&lt;/code&gt;, let's grab this new &lt;code&gt;div&lt;/code&gt; element by its id and put it in a constant. Let's name this constant &lt;code&gt;jsContainer&lt;/code&gt;. We can use &lt;code&gt;document.getElementById&lt;/code&gt; to grab the &lt;code&gt;div&lt;/code&gt; from HTML:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const jsContainer = document.getElementById("js");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To control the content of this &lt;code&gt;div&lt;/code&gt;, we can use the &lt;code&gt;innerHTML&lt;/code&gt; setter call on the &lt;code&gt;div&lt;/code&gt; element directly. We can use this call to supply any HTML template that we want inserted in the DOM. Let's insert a &lt;code&gt;div&lt;/code&gt; element with a class of "demo" and the string "Hello JS" as its content:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;jsContainer.innerHTML = `
  &amp;lt;div class="demo"&amp;gt;
    Hello JS
  &amp;lt;/div&amp;gt;
`;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Make sure this works in the browser. You should see the “Hello JS” line on the screen now.&lt;/p&gt;

&lt;p&gt;This demo div is our User Interface so far. It’s a very simple one. We just output a text for the user to see.&lt;/p&gt;

&lt;p&gt;Both &lt;code&gt;document.getElementById&lt;/code&gt; and &lt;code&gt;element.innerHTML&lt;/code&gt; are actually part of the native DOM Web API. We are communicating with the browser directly here using the supported APIs of the Web platform. When we write React code, however, we use the React API instead, and we let React communicate with the browser using the DOM Web API.&lt;/p&gt;

&lt;p&gt;React acts like our &lt;em&gt;agent&lt;/em&gt; for the browser, and we &lt;em&gt;mostly&lt;/em&gt; need to communicate with just React, our agent, and not the browser itself. I say mostly because there are cases where we still need to communicate with the browser, but those are rare.&lt;/p&gt;

&lt;p&gt;To create the exact same User Interface that we have so far but with React API this time, let’s create another &lt;code&gt;div&lt;/code&gt; element and give it an id of &lt;code&gt;"react"&lt;/code&gt;. In &lt;code&gt;index.html&lt;/code&gt;, right under the &lt;code&gt;div#js&lt;/code&gt; element, add:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div id="react"&amp;gt;&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, in &lt;code&gt;script.js&lt;/code&gt;, create a new container constant for the new &lt;code&gt;div&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const reactContainer = document.getElementById("react");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This container will be the only call we make to the native web API. ReactDOM needs this container to know where to host our application in the DOM.&lt;/p&gt;

&lt;p&gt;With the react container identified, we can now use the ReactDOM library to &lt;code&gt;render&lt;/code&gt; React's version of the HTML template to this container:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ReactDOM.render(
  /* TODO: React's version of the HTML template */,
  reactContainer
)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What we’re going to do next is your first milestone in truly understanding the React library. Remember when I told you that with React we write HTML using JavaScript? This is exactly what we are going to do next.&lt;/p&gt;

&lt;p&gt;To write our simple HTML User Interface, we are going to use JavaScript calls to React API, and by the end of the example you’ll have a better picture about the reason for doing so.&lt;/p&gt;

&lt;p&gt;Instead of working with strings (as we did in the native JavaScript example above), in React, we work with &lt;em&gt;objects&lt;/em&gt;. Any HTML string will be represented as an object using a &lt;code&gt;React.createElement&lt;/code&gt; call (which is the core function in the React API).&lt;/p&gt;

&lt;p&gt;Here’s the equivalent HTML User Interface we have so far with React:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ReactDOM.render(
    React.createElement(
      "div",
      { className: "demo" },
      "Hello React"
    ),
    reactContainer
  );
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;React.createElement&lt;/code&gt; has many arguments:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The first argument is the HTML tag, which is &lt;code&gt;div&lt;/code&gt; in our example.&lt;/li&gt;
&lt;li&gt;The second argument is an object that represents any attributes we want this tag to have. To match the native JS example we used &lt;code&gt;{ className: "demo" }&lt;/code&gt; which translates to &lt;code&gt;class="demo"&lt;/code&gt;. Note how we used &lt;code&gt;className&lt;/code&gt; instead of &lt;code&gt;class&lt;/code&gt; in the attributes because with React it's all JavaScript that matches the Web API, not HTML itself.&lt;/li&gt;
&lt;li&gt;The third argument is the content of the element. We’ve put a “Hello React” string in there.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We can test this now. The browser should render both “Hello JS” and “Hello React”. Let’s style the demo divs as a box, using this CSS, just so that we can visually split the screen. In &lt;code&gt;index.html&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;style media="screen"&amp;gt;
  .demo {
    border: 1px solid #ccc;
    margin: 1em;
    padding: 1em;
  }
&amp;lt;/style&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--nUK4xGiw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1600/1%2ATwcqWtECXp6OA0mowRcvEA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--nUK4xGiw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1600/1%2ATwcqWtECXp6OA0mowRcvEA.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We now have two nodes, one being controlled with the DOM Web API directly, and another being controlled with the React API (which in turn uses the DOM Web API). The only major difference between the ways we are building these two nodes in the browser is that in the JS version we used a string to represent the content, while in the React version we used pure JavaScript calls and represented the content with an object instead of a string.&lt;/p&gt;

&lt;p&gt;No matter how complicated the HTML User Interface is going to get, when using React, every HTML element will be represented with a JavaScript object using a &lt;code&gt;React.createElement&lt;/code&gt; call.&lt;/p&gt;

&lt;p&gt;Let’s now add some more features to our simple User Interface. Let’s add a text box to read input from the user.&lt;/p&gt;

&lt;p&gt;To nest elements in our HTML template, it’s straight forward in the JS version because it’s just HTML. For example, to make the demo &lt;code&gt;div&lt;/code&gt; render an &lt;code&gt;&amp;lt;input /&amp;gt;&lt;/code&gt; element, we simply add it to the content:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;jsContainer.innerHTML = `
  &amp;lt;div class="demo"&amp;gt;
    Hello JS
    &amp;lt;input /&amp;gt;
  &amp;lt;/div&amp;gt;
`;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can do the same with React by adding more arguments after the 3rd argument for &lt;code&gt;React.createElement&lt;/code&gt;. To match what we did in the native JS example, we can add a 4th argument that is another &lt;code&gt;React.createElement&lt;/code&gt; call that renders an &lt;code&gt;input&lt;/code&gt; element (remember, every HTML element is an object):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ReactDOM.render( React.createElement( "div", { className: "demo" }, "Hello React", React.createElement("input") ), reactContainer);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;At this point, if you’re questioning what we’re doing and thinking “this is complicating a simple process”, you are totally right! But there is a very good reason for what we’re doing. Keep reading.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Let’s also render a timestamp in both versions. In the JS version, let’s put the timestamp in a paragraph element. We can use a call to &lt;code&gt;new Date()&lt;/code&gt; to display a simple timestamp:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;jsContainer.innerHTML = `
  &amp;lt;div class="demo"&amp;gt;
    Hello JS
    &amp;lt;input /&amp;gt;
    &amp;lt;p&amp;gt;${new Date()}&amp;lt;/p&amp;gt;
  &amp;lt;/div&amp;gt;
`;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To do the same in React, we add a 5th argument to the top-level &lt;code&gt;div&lt;/code&gt; element. This new 5th argument is another &lt;code&gt;React.createElement&lt;/code&gt; call, this time using a &lt;code&gt;p&lt;/code&gt; tag, with no attributes, and the &lt;code&gt;new Date()&lt;/code&gt; string for content:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ReactDOM.render(
  React.createElement(
    "div",
    { className: "demo" },
    "Hello React",
    React.createElement("input"),
    React.createElement(
      "p",
      null,
      new Date().toString()
    )
  ),
  reactContainer
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Both JS and React versions are still rendering the exact same HTML in the browser.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ai0o6ZK7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1600/1%2AfLaNHWXUJh4ICEvMXByvwg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ai0o6ZK7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1600/1%2AfLaNHWXUJh4ICEvMXByvwg.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see, so far, using React is actually a lot harder than the simple and familiar native way. What is it that React does so well that’s worth giving up the familiar HTML and having to learn a new API to write what can be simply written in HTML? The answer is not about rendering the first HTML view, it’s about what we need to do to update any existing view in the DOM.&lt;/p&gt;

&lt;p&gt;So, let’s do an update operation on the DOM we have so far. Let’s simply make the timestamp tick every second.&lt;/p&gt;

&lt;p&gt;We can easily repeat a JavaScript function call in a browser using the &lt;code&gt;setInterval&lt;/code&gt; Web timer API. So, let's put all of our DOM manipulations for both JS and React versions in a function, call it &lt;code&gt;render&lt;/code&gt;, and use it in a &lt;code&gt;setInterval&lt;/code&gt; call to make it repeat every second.&lt;/p&gt;

&lt;p&gt;Here’s the full final code in &lt;code&gt;script.js&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const jsContainer = document.getElementById("js");
const reactContainer = document.getElementById("react");

const render = () =&amp;gt; {
  jsContainer.innerHTML = `
    &amp;lt;div class="demo"&amp;gt;
      Hello JS
      &amp;lt;input /&amp;gt;
      &amp;lt;p&amp;gt;${new Date()}&amp;lt;/p&amp;gt;
    &amp;lt;/div&amp;gt;
  `;

ReactDOM.render(
    React.createElement(
      "div",
      { className: "demo" },
      "Hello React ",
      React.createElement("input"),
      React.createElement(
        "p",
        null,
        new Date().toString()
      )
    ),
    reactContainer
  );
}

setInterval(render, 1000);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When we refresh the browser now, the timestamp string should be ticking every second in both versions. We are now updating our User Interface in the DOM.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;This is the moment when React will potentially blow your mind.&lt;/em&gt; If you try to type something in the text box of the JS version, you won’t be able to. This is very much expected because we’re basically throwing away the whole DOM node on every tick and regenerating it. However, if you try to type something in the text box that’s rendered with React, you can certainly do so!&lt;/p&gt;

&lt;p&gt;Although the whole React rendering code is within our ticking timer, React is changing only the timestamp paragraph and not the whole DOM node. This is why the text input box was not regenerated and we were able to type in it.&lt;/p&gt;

&lt;p&gt;You can see the different ways we’re updating the DOM visually if you inspect the two DOM nodes in a Chrome dev tools elements panel. The Chrome div tools highlights any HTML elements that get updated. You’ll see how we are regenerating the whole “js” div on every tick, while React is smartly only regenerating the paragraph with the timestamp string.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--JwzUxlT8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://cdn-images-1.medium.com/max/1600/1%2A9RGpVv6Mwjl6LApR7vsYqA.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--JwzUxlT8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://cdn-images-1.medium.com/max/1600/1%2A9RGpVv6Mwjl6LApR7vsYqA.gif" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;React has a smart &lt;em&gt;diffing&lt;/em&gt; algorithm that it uses to only regenerate in its DOM node what actually &lt;em&gt;needs&lt;/em&gt; to be regenerated while it keeps everything else as is. This diffing process is possible because of React’s virtual DOM and the fact that we have a representation of our User Interface in memory (because we wrote in JavaScript).&lt;/p&gt;

&lt;p&gt;Using the virtual DOM, React keeps the last DOM version in memory and when it has a new DOM version to take to the browser, that new DOM version will also be in memory, so React can compute the difference between the new and the old versions (in our case, the difference is the timestamp paragraph).&lt;/p&gt;

&lt;p&gt;React will then instruct the browser to update only the computed diff and not the whole DOM node. No matter how many times we regenerate our interface, React will take to the browser only the new “partial” updates.&lt;/p&gt;

&lt;p&gt;Not only is this method a lot more efficient, but it also removes a big layer of complexity for the way we &lt;em&gt;think&lt;/em&gt; about updating User Interfaces. Having React do all the computations about whether we should update the DOM or not enables us to focus on thinking about our data (state) and the way to describe a User Interface for it.&lt;/p&gt;

&lt;p&gt;We then manage the updates on our data as needed without worrying about the steps needed to reflect these updates on the actual User Interface in the browser (because we know React will do exactly that and it will do that in an efficient way!)&lt;/p&gt;

&lt;p&gt;Thanks for reading! You can view the source code of my demo &lt;a href="https://github.com/jscomplete/react-virtual-dom-demo/tree/master/demo"&gt;here&lt;/a&gt;, and you can see the demo running &lt;a href="https://jscomplete.github.io/react-virtual-dom-demo/demo/"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;This article was originally published &lt;a href="https://medium.freecodecamp.org/yes-react-is-taking-over-front-end-development-the-question-is-why-40837af8ab76"&gt;here&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;




&lt;p&gt;Learning React or Node? checkout my books ;)&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://jscomplete.com/learn-react-js-by-building-games"&gt;Learn React.js by Building Games&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://jscomplete.com/learn-node-js-beyond-the-basics"&gt;Node.js Beyond the Basics&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I create &lt;strong&gt;online courses&lt;/strong&gt; for &lt;a href="https://app.pluralsight.com/profile/author/samer-buna"&gt;Pluralsight&lt;/a&gt;, &lt;a href="https://www.lynda.com/Samer-Buna/7060467-1.html"&gt;Lynda&lt;/a&gt; and &lt;a href="https://jscomplete.com/samer-buna"&gt;many more&lt;/a&gt;. I also do &lt;strong&gt;online and onsite training&lt;/strong&gt; for teams covering beginner to advanced levels in JavaScript, Node.js, React.js, and GraphQL. Email &lt;a href="//mailto:training@agilelabs.com"&gt;training@agilelabs.com&lt;/a&gt; if you want to book a session for your team.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>react</category>
      <category>programming</category>
    </item>
    <item>
      <title>How to write your first React.js component</title>
      <dc:creator>Samer Buna</dc:creator>
      <pubDate>Tue, 01 May 2018 16:52:24 +0000</pubDate>
      <link>https://forem.com/samerbuna/how-to-write-your-first-reactjs-component-191i</link>
      <guid>https://forem.com/samerbuna/how-to-write-your-first-reactjs-component-191i</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;This article is a write-up of part of my Pluralsight course — &lt;a href="https://www.pluralsight.com/courses/react-js-getting-started" rel="noopener noreferrer"&gt;React.js: Getting Started&lt;/a&gt;. I cover similar content in video format there.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The most important concept to understand in React.js is the component. A React component can be one of two types. It can be either a &lt;strong&gt;function&lt;/strong&gt; component or a &lt;strong&gt;class&lt;/strong&gt; component. Sometimes you will hear different terms to describe these two types, like &lt;strong&gt;stateless&lt;/strong&gt; and &lt;strong&gt;stateful&lt;/strong&gt;. Function components are also often associated with the &lt;strong&gt;presentational&lt;/strong&gt; concept. I will refer to them in this article as function components and class components.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A function component&lt;/strong&gt; is the simplest form of a React component. It is a simple function with a simple contract:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frx5kldgpr4o68t8sv0j8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frx5kldgpr4o68t8sv0j8.png" width="800" height="450"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Screenshot captured from my Pluralsight course — &lt;a href="https://www.pluralsight.com/courses/react-js-getting-started" rel="noopener noreferrer"&gt;React.js: Getting Started&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The function component receives an object of properties which is usually named &lt;strong&gt;props.&lt;/strong&gt; It returns what looks like HTML, but is really a special JavaScript syntax called &lt;a href="https://facebook.github.io/react/docs/introducing-jsx.html" rel="noopener noreferrer"&gt;JSX&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;A &lt;strong&gt;class component&lt;/strong&gt; is a more featured way to define a React component. It also acts like a function that receives props, but that function also considers a private internal state as additional input that controls the returned JSX.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fipq1wl27a0pie1ciseji.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fipq1wl27a0pie1ciseji.png" width="800" height="450"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Screenshot captured from my Pluralsight course — &lt;a href="https://www.pluralsight.com/courses/react-js-getting-started" rel="noopener noreferrer"&gt;React.js: Getting Started&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;This private internal state is what gives React its &lt;strong&gt;reactive&lt;/strong&gt; nature. When the state of a class component changes, React will re-render that component in the browser.&lt;/p&gt;

&lt;p&gt;The State and Props objects have one important difference. Inside a class component, the State object can be changed while the Props object represents fixed values. Class components can only change their internal state, not their properties. This is a core idea to understand in React and this article will have an example of that.&lt;/p&gt;

&lt;p&gt;Let’s look at an actual example of a component. A very simple one, without any input and with a simple &lt;code&gt;h1&lt;/code&gt; in a &lt;code&gt;div&lt;/code&gt; output.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5vz5a0uyi1f7w1n2jl3o.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5vz5a0uyi1f7w1n2jl3o.png" width="800" height="357"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Screenshot captured from my Pluralsight course — &lt;a href="https://www.pluralsight.com/courses/react-js-getting-started" rel="noopener noreferrer"&gt;React.js: Getting Started&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;On the left side, the component is written in the special JSX syntax.&lt;/p&gt;

&lt;p&gt;JSX allows us to describe our User Interfaces (UIs) in a syntax very close to the HTML that we are used to. It is, however, optional. React can be used without JSX, as you can see on the right side. In fact, React just compiles the JSX you see on the left to the pure JavaScript you see on the right. Then it works with compiled JavaScript in the browser.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;React.createElement&lt;/code&gt; call on the right side is a JavaScript representation of the Document Object Model (&lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Document_Object_Model" rel="noopener noreferrer"&gt;DOM&lt;/a&gt;). React efficiently translates it into DOM operations that it performs in the browser.&lt;/p&gt;
&lt;h3&gt;
  
  
  Let’s write a React component.
&lt;/h3&gt;

&lt;p&gt;I will be using jsComplete’s &lt;a href="https://jscomplete.com/react" rel="noopener noreferrer"&gt;React Playground&lt;/a&gt; for the examples in this article. It’s a tool where you can test your JavaScript and React code right in the browser. There is no need to install or configure anything.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://jscomplete.com/react" rel="noopener noreferrer"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frtizq8anfuiux15leeiv.png" width="800" height="391"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The tool has a simple two-panels interface. The left panel is the editor where you write your JavaScript and React code. The latest version of both React and ReactDOM are already pre-loaded there. The editor also understands the JSX extension and all the modern features in JavaScript. This will allow us to focus on the React API itself rather than configuring and compiling a React application.&lt;/p&gt;

&lt;p&gt;The right panel is the preview panel. You have a pre-defined &lt;code&gt;mountNode&lt;/code&gt; element in the editor. When you execute your JavaScript code, anything you put in the &lt;code&gt;mountNode&lt;/code&gt; element shows up in the preview panel. The preview panel will also show any errors you encounter when you execute your code. The playground is also a simple JavaScript &lt;em&gt;REPL&lt;/em&gt; (Run, Eval, Print, Loop) where you can test quick JavaScript functions and expressions. To execute the code at any time press &lt;code&gt;CTRL+Enter&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Try the following, for example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mountNode.innerHTML = 'Hello!!';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or the simple REPL mode&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;3 == '3'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To create a React component, define a new function. Let’s make that function return an HTML button element:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function Button() { 
  return ( 
    &amp;lt;button&amp;gt;Go&amp;lt;/button&amp;gt; 
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What we returned here looks like HTML but remember that it is not. It is going to be compiled into JavaScript. The actual JavaScript that the browser sees when we use this button element in JSX is a call to the &lt;code&gt;React.createElement&lt;/code&gt;function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function Button() { 
  return ( 
    React.createElement("button", null, "Go") 
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;While you can use React this way without JSX, it would be a lot harder to code and maintain. So, let’s stick with JSX.&lt;/p&gt;

&lt;p&gt;The function above is a complete and very simple React component. Let’s use it!&lt;/p&gt;

&lt;p&gt;We use a component by mounting it in the browser. The function designed to do that is &lt;code&gt;ReactDOM.render&lt;/code&gt;, which takes in two arguments:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The first is the component to render, in our case it is &lt;code&gt;Button&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The second argument is the element in which this component should be rendered. In the REPL’s environment we can use the special &lt;code&gt;mountNode&lt;/code&gt; variable.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ReactDOM.render(&amp;lt;Button /&amp;gt;, mountNode);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://jscomplete.com/repl?j=Skl5GngO-" rel="noopener noreferrer"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffr5pn2nb99uewpm191g6.png" width="800" height="128"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;All code examples in this article have a link in the screenshot caption where you can edit the example at jsComplete REPL.&lt;/p&gt;

&lt;p&gt;A React function component receives as its first argument the &lt;code&gt;props&lt;/code&gt; object. This argument allows us to make the component re-usable. For example, instead of hard-coding the “Go” label of the button above, we can pass the &lt;code&gt;Button&lt;/code&gt; component a &lt;code&gt;label&lt;/code&gt; attribute, like we do with regular HTML elements:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ReactDOM.render(&amp;lt;Button label="Save" /&amp;gt;, mountNode);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then we can access this attribute inside the component with a curly bracket for &lt;code&gt;props.label&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function Button(props) { 
  return ( 
    &amp;lt;button&amp;gt;{props.label}&amp;lt;/button&amp;gt; 
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;props&lt;/code&gt; argument is an object that holds all the values that were passed to the component when it was rendered.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://jscomplete.com/repl?j=ByQm4nl_Z" rel="noopener noreferrer"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgotnqbdvta8jj968ek65.png" width="800" height="124"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Making the component interactive
&lt;/h3&gt;

&lt;p&gt;We have a button element and it is rendered through a React component.&lt;/p&gt;

&lt;p&gt;Let’s now add some interactivity to this so-far boring example. Let’s make that button element increment a counter value on every click and display that value as the button label itself. So the label of this button is going to start with the number 1 and when the user clicks the button its label will change to 2, 3, 4 and so on.&lt;/p&gt;

&lt;p&gt;Since this is something that needs to be reflected in the component rendered output, it belongs to the state of the component. We need the component to re-render itself every time the counter changes. We cannot use a property here because a component props cannot be changed. By using the special React state object, we will be utilizing React’s &lt;strong&gt;reactive&lt;/strong&gt; nature and we will not need to worry about &lt;strong&gt;how&lt;/strong&gt; to take the changes to the browser. React will do that for us.&lt;/p&gt;

&lt;p&gt;But, our Button component is currently a function component. Function components cannot have state, so we need to upgrade this component to a class component first.&lt;/p&gt;

&lt;p&gt;This is very simple. We first define a class that extends &lt;code&gt;React.Component&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Button extends React.Component { }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In that class we define a &lt;code&gt;render&lt;/code&gt; function, which returns the component’s JSX; the HTML button in our case.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;render() { 
  return ( 
    &amp;lt;button&amp;gt;1&amp;lt;/button&amp;gt; 
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is a little bit more code, but we can now use a private state on the Button component!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://jscomplete.com/repl?j=BJCWI2gd-" rel="noopener noreferrer"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F69zn9hwnfss9ystmyslj.png" width="800" height="147"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To use a state object we first need to initialize it. The state object is a simple instance property, so we can initialize it inside the constructor function of the &lt;code&gt;Button&lt;/code&gt; class. We just define the normal constructor function (which receives a &lt;code&gt;props&lt;/code&gt; object in React) and call the &lt;code&gt;super&lt;/code&gt; method to honor the inheritance of the component.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;constructor(props) { 
  super(props); 
  this.state = { counter: 1 };
 }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After that, we initialize &lt;code&gt;this.state&lt;/code&gt; to whatever we want. The keys of this state object are the various elements of the state. For our case, we need a &lt;code&gt;counter&lt;/code&gt; state, which starts from 1.&lt;/p&gt;

&lt;p&gt;Inside the render function, since we can write any JavaScript expression within curly brackets, we can read the value of the new &lt;code&gt;counter&lt;/code&gt; state element that we initialized on the state using &lt;code&gt;this.state.counter&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;render() { 
  return ( 
    &amp;lt;button&amp;gt;{this.state.counter}&amp;lt;/button&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The “&lt;code&gt;this&lt;/code&gt;” keyword refers to the component instance we are handing off to &lt;code&gt;ReactDOM&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://jscomplete.com/repl?j=SJfwu2xuZ" rel="noopener noreferrer"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0dxis00k6axicg4s40ii.png" width="800" height="213"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can try and change that counter state to see how the button will render the values you put on the state.&lt;/p&gt;

&lt;p&gt;There is another shorter syntax to define the initial state, which is to simply use a class property without a constructor call:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Button extends React.Component { 
  state = { counter: 1 }; 
  render() { 
    return ( 
      &amp;lt;button&amp;gt;{this.state.counter}&amp;lt;/button&amp;gt; 
    ); 
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is not yet part of the official JavaScript language but it will be soon. The syntax works at the jsComplele REPL playground because that tool is using Babel to transpile it to the supported JavaScript that the browser will understand.&lt;/p&gt;

&lt;p&gt;When you configure your own React application you’ll have to use something like Babel anyway to compile JSX into JavaScript. It is an easy win to also include and use the JavaScript features that are well on their way to becoming an official part of the language.&lt;/p&gt;

&lt;p&gt;In the &lt;code&gt;Button&lt;/code&gt; example so far, we have a state object and an HTML button element that displays a counter value that we initialized on the state. Now we need to change that value when we click the button. We need to define a click handler on that button.&lt;/p&gt;

&lt;p&gt;React comes with normalized events that are easy to use. For this case, we need the &lt;code&gt;onClick&lt;/code&gt; event, which we define on the HTML button element itself:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function F() {}

&amp;lt;button onClick={F} /&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Unlike DOM event handlers, which use a string, React event handlers use an actual JavaScript function. This function can be a global one (like &lt;code&gt;F&lt;/code&gt; above), or an inline function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;button onClick={() =&amp;gt; {}} /&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;However, the standard practice is to define a function on the class component itself. Let’s call it &lt;code&gt;handleClick&lt;/code&gt; and we can define it on the component as an instance property:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Button extends React.Component { 
  state = { counter: 1 }; 

  handleClick = () =&amp;gt; { 
    console.log('Button is clicked!!'); 
  }; 

  render() { 
    return ( 
      &amp;lt;button onClick={this.handleClick}&amp;gt; {this.state.counter} &amp;lt;/button&amp;gt; 
    ); 
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We are using the modern class field syntax, which allows us to use arrow functions that are bound to the component instance. &lt;code&gt;handleClick&lt;/code&gt; will now act as a prototype function on this class. Inside &lt;code&gt;handleClick&lt;/code&gt; the keyword “&lt;code&gt;this&lt;/code&gt;” refers to the component instance that we are mounting in the DOM.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;handleClick&lt;/code&gt;’s job is easy: read the current counter value from the state object using &lt;code&gt;this.state.counter&lt;/code&gt;. Then increment this value and update the component state with the new incremented value.&lt;/p&gt;

&lt;p&gt;We can use React’s built-in &lt;code&gt;setState&lt;/code&gt; method, which is available on every class component instance, to update a component state.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://jscomplete.com/repl?j=Sy-u46l_Z" rel="noopener noreferrer"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbwxgsh4lc68fkpl43yqx.png" width="800" height="278"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The button will now increment its label on every click.&lt;/p&gt;

&lt;p&gt;This was simple and powerful! We defined an event handler for the &lt;code&gt;onClick&lt;/code&gt; method. Every time the user clicks the button the &lt;code&gt;handleClick&lt;/code&gt; function will be executed. The function reads the current state of the counter value, increments it, and then sets the state to the new incremented value. React takes care of all the rendering needed after these changes so you do not have to worry about that.&lt;/p&gt;

&lt;p&gt;Note that we did not update the state object directly. We have to use React’s &lt;code&gt;setState&lt;/code&gt; method when we want to update any element on the state. You can’t for example do this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// WRONG:
this.state.counter = this.state.counter + 1;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;React’s &lt;code&gt;setState&lt;/code&gt; method is an asynchronous one which schedules an update. Multiple &lt;code&gt;setState&lt;/code&gt; calls might potentially be batched for performance. Since we are both reading and writing to the state object inside the &lt;code&gt;handleClick&lt;/code&gt; function, we could hit a race condition. The general rule of thumb is whenever you need to update the state using a value from the current state, use the other contract of the &lt;code&gt;setState&lt;/code&gt; method. This receives a function reference instead of an object as its first argument:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;this.setState((prevState) =&amp;gt; {});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This function receives a &lt;code&gt;prevState&lt;/code&gt; object that we can confidently use without worrying about race conditions. The function returns the object that we want React to use to set the state. Our &lt;code&gt;counter&lt;/code&gt; value example above becomes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;this.setState((prevState) =&amp;gt; ({
  counter: prevState.counter + 1 
}));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You only need to use this second syntax of &lt;code&gt;setState&lt;/code&gt; if your update depends on the current state. However, it may be a good idea to make a habit of always using the second function-argument syntax.&lt;/p&gt;

&lt;p&gt;Here is the final code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Button extends React.Component {
  state = { counter: 1 };

  handleClick = () =&amp;gt; {
    this.setState((prevState) =&amp;gt; ({
      counter: prevState.counter + 1 
    }));
  };

  render() {
    return (
      &amp;lt;button onClick={this.handleClick}&amp;gt;
        {thi
        s.state.counter}
      &amp;lt;/button&amp;gt;
    );
  }
}

ReactDOM.render(&amp;lt;Button /&amp;gt;, mountNode);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://jscomplete.com/repl?j=rJgDsTgdb" rel="noopener noreferrer"&gt;Test it out&lt;/a&gt; and if you have any questions let me know.&lt;/p&gt;




&lt;p&gt;Learning React or Node? Checkout my books:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="http://amzn.to/2peYJZj" rel="noopener noreferrer"&gt;Learn React.js by Building Games&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://amzn.to/2FYfYru" rel="noopener noreferrer"&gt;Node.js Beyond the Basics&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;This article was originally published &lt;a href="https://medium.freecodecamp.org/how-to-write-your-first-react-js-component-d728d759cabc" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
    </item>
    <item>
      <title>React.js Frequently Faced Problems</title>
      <dc:creator>Samer Buna</dc:creator>
      <pubDate>Tue, 20 Feb 2018 16:09:24 +0000</pubDate>
      <link>https://forem.com/pluralsight/reactjs-frequently-facedproblems--l5g</link>
      <guid>https://forem.com/pluralsight/reactjs-frequently-facedproblems--l5g</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fne8nz6shmev15pypfs20.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fne8nz6shmev15pypfs20.png" width="800" height="226"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;At &lt;a href="https://jscomplete.com" rel="noopener noreferrer"&gt;&lt;strong&gt;jsComplete&lt;/strong&gt;&lt;/a&gt;, we manage a &lt;a href="https://slackin-bfcnswvsih.now.sh/" rel="noopener noreferrer"&gt;slack account&lt;/a&gt; dedicated to helping code learners get unstuck. We receive some interesting problems every now and then but most of the asked questions are for common problems. I am creating this resource to write detailed instructions for the common problems beginner React.js learners usually face so that I can refer them to here instead of typing it over and over.&lt;/p&gt;




&lt;h4&gt;
  
  
  1 — Not starting a component name with a capital letter
&lt;/h4&gt;

&lt;p&gt;A React component must have a name which starts with a capital letter.&lt;/p&gt;

&lt;p&gt;If the component name does not start with a capital letter, the component use will be treated as a &lt;em&gt;built-in&lt;/em&gt; element such as a &lt;code&gt;div&lt;/code&gt; or &lt;code&gt;span&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;greeting&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Component&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
  &lt;span class="c1"&gt;// ...&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you try to render &lt;code&gt;&amp;lt;greeting /&amp;gt;&lt;/code&gt;, React will ignore the above and you will get a warning:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Warning: The tag &amp;lt;greeting&amp;gt; is unrecognized in this browser. 
If you meant to render a React component, 
start its name with an uppercase letter.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The bigger problem here is when you decide to name your component &lt;code&gt;button&lt;/code&gt; or &lt;code&gt;img&lt;/code&gt;. React will ignore your component and just render a vanilla HTML &lt;code&gt;button&lt;/code&gt; or &lt;code&gt;img&lt;/code&gt; tag.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgwqbu4c83x7tbsp74hkn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgwqbu4c83x7tbsp74hkn.png" width="800" height="244"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Note how the “My Awesome Button” was not rendered above and React just rendered an empty HTML button element. React will not warn you in this case.&lt;/p&gt;

&lt;h4&gt;
  
  
  2 — Using single quotes instead of back-ticks
&lt;/h4&gt;

&lt;p&gt;Strings created with back-ticks (`...`) are different from strings created with single quotes ('...').&lt;/p&gt;

&lt;p&gt;&lt;em&gt;On most keyboards, the back-tick (`) character can be typed using the key above the &lt;code&gt;tab&lt;/code&gt; key&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;We create a string using back-ticks when we need to include dynamic expressions inside that string (without resorting to string concatenation).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="s2"&gt;`This is a string template literal that can include expressions`&lt;/span&gt;

&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;This is just a string, you cannot include expressions here&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let’s say you want a string that always reports the current time: &lt;br&gt;&lt;br&gt;
&lt;code&gt;“Time is ...”&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Current time string&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;time&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;toLocaleTimeString&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="c1"&gt;// When using regular strings (single or double quotes),&lt;/span&gt;
&lt;span class="c1"&gt;// you need to use string concatenation:&lt;/span&gt;
&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Time is &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;time&lt;/span&gt;

&lt;span class="c1"&gt;// When using back-ticks,&lt;/span&gt;
&lt;span class="c1"&gt;// you can inject the time in the string using ${}&lt;/span&gt;
&lt;span class="s2"&gt;`Time is &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;time&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Also, when using string literals (with back-ticks), you can create a string that spans multiple lines:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;template&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;`I

CAN

SPAN

Multiple Lines`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can’t do that with regular strings.&lt;/p&gt;




&lt;h4&gt;
  
  
  3— Using React.PropTypes
&lt;/h4&gt;

&lt;p&gt;The &lt;code&gt;PropTypes&lt;/code&gt; object was removed from React. It used to be available as &lt;code&gt;React.PropTypes&lt;/code&gt; but you cannot use that anymore.&lt;/p&gt;

&lt;p&gt;Instead, you need to:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Add the new &lt;strong&gt;prop-types&lt;/strong&gt; package to your project: &lt;code&gt;npm install prop-types&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Import it: &lt;code&gt;import PropTypes from 'prop-types'&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Then you can use it. For example: &lt;code&gt;PropTypes.string&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;If you incorrectly use &lt;code&gt;React.PropTypes&lt;/code&gt;, you’ll get errors like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;TypeError: Cannot read property 'string' of undefined
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h4&gt;
  
  
  4 — Not using the right versions of what a tutorial is using
&lt;/h4&gt;

&lt;p&gt;When watching or reading content about coding and following up with the examples they present, make sure that you are using correct versions of the tools the content is using. Usually, using the latest version of each tool is a safe bet, but if the content is a bit old you might run into some deprecation issues.&lt;/p&gt;

&lt;p&gt;To be safe, stick with the major versions for the used tools. For example, if the tutorial is using React 16, don’t follow up using React 15.&lt;/p&gt;

&lt;p&gt;This is especially important for Node.js as well. You will face major problems if you use an older version of Node. For example, if you’re following along with some tutorial that uses &lt;code&gt;Object.values&lt;/code&gt; and you are using Node 6.x, that method did not exist back then. You need Node 7.x or higher.&lt;/p&gt;




&lt;h4&gt;
  
  
  5— Confusing functions with classes
&lt;/h4&gt;

&lt;p&gt;Can you tell what's wrong with the following code?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Numbers&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Component&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;arrayOfNumbers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;_&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="c1"&gt;// ...&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The code above is invalid because inside the body of a JavaScript class you don’t have the freedom of doing just about anything. You can only define methods and properties using limited syntax.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;This is a bit confusing because the&lt;/em&gt; &lt;code&gt;{}&lt;/code&gt; &lt;em&gt;used in the class syntax looks like the plain-old block scope, but it’s not.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Inside a function-based component, you DO have the freedom of doing just about anything:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Totally Okay:&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nb"&gt;Number&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;arrayOfNumbers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;_&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="c1"&gt;// ...&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h4&gt;
  
  
  6 — Passing numbers as strings
&lt;/h4&gt;

&lt;p&gt;You can pass a prop value with a string:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Greeting&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"World"&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you need to pass a numeric value, don’t use strings:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Don't do this&lt;/span&gt;
&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Greeting&lt;/span&gt; &lt;span class="na"&gt;counter&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"7"&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Instead, use curly braces to pass an actual numeric value:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Do this instead&lt;/span&gt;
&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Greeting&lt;/span&gt; &lt;span class="na"&gt;counter&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Using &lt;code&gt;{7}&lt;/code&gt;, inside the &lt;code&gt;Greeting&lt;/code&gt; component, &lt;code&gt;this.props.counter&lt;/code&gt; will have the actual numeric &lt;code&gt;7&lt;/code&gt; value and it will be safe to do mathematical operations on that. If you pass it as &lt;code&gt;“7”&lt;/code&gt; and then treat it as a number, you might run into unexpected results.&lt;/p&gt;




&lt;h4&gt;
  
  
  7 — Forgetting that another app instance is still using the same port
&lt;/h4&gt;

&lt;p&gt;To run a web server, your need to use a host (like 127.0.0.1) and a port (like 8080) to make the server listen for request on a valid http address.&lt;/p&gt;

&lt;p&gt;Once the web server is running successfully, it has control over that port. You cannot use the same port for anything else. The port will be busy.&lt;/p&gt;

&lt;p&gt;If you try to run the same server in another terminal, you’ll get an error that the port is “in use”. Something like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Error: listen EADDRINUSE 127.0.0.1:8080
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Be aware that sometimes a web server might be running in the &lt;em&gt;background&lt;/em&gt; or inside a detached screen/tmux session. You don’t see it, but it is still occupying the port. To restart your server, you need to “kill” the one that’s still running.&lt;/p&gt;

&lt;p&gt;To identify the process that’s using a certain port, you can either use a command like &lt;code&gt;ps&lt;/code&gt; (and &lt;code&gt;grep&lt;/code&gt; for something about your app) or if you know the port number you can use the &lt;code&gt;lsof&lt;/code&gt; command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;lsof -i :8080
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h4&gt;
  
  
  8 — Forgetting to create environment variables
&lt;/h4&gt;

&lt;p&gt;Some projects depend on the existence of shell environment variables in order to start. If you run these projects without the needed environment variables, they will try to use undefined values for them and will potentially give you some cryptic errors.&lt;/p&gt;

&lt;p&gt;For example, if a project connects to a database like MongoDB, chances are it uses an environment variable like &lt;code&gt;process.env.MONGO_URI&lt;/code&gt; to connect to it. This allows the project to be used with different MongoDB instances in different environments.&lt;/p&gt;

&lt;p&gt;To locally run the project that connects to a MongoDB, you have to export a &lt;code&gt;MONGO_URI&lt;/code&gt; environment variable first. For example, if you have a local MongoDB running on port &lt;code&gt;27017&lt;/code&gt;, you would need to do this before running the project:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export MONGO_URI="mongodb://localhost:27017/mydb"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can &lt;em&gt;grep&lt;/em&gt; the project source code for &lt;code&gt;process.env&lt;/code&gt; to figure out what environment variables it needs in order to work correctly.&lt;/p&gt;

&lt;h4&gt;
  
  
  9— Confusing curly braces {} with parentheses ()
&lt;/h4&gt;

&lt;p&gt;Instead of:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
  &lt;span class="nf"&gt;something&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You need:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; 
  &lt;span class="nf"&gt;something&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first one will try (and fail) to return an object while the second one will correctly call the &lt;code&gt;something()&lt;/code&gt; function and return what that function returns.&lt;/p&gt;

&lt;p&gt;Since any &lt;code&gt;&amp;lt;tag&amp;gt;&lt;/code&gt; in JSX will translate to a function call, this problem applies when returning any JSX.&lt;/p&gt;

&lt;p&gt;This problem is also common in arrow functions’ &lt;em&gt;short&lt;/em&gt; syntax.&lt;/p&gt;

&lt;p&gt;Instead of:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Greeting&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
  &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; 
    &lt;span class="nx"&gt;Hello&lt;/span&gt; &lt;span class="nx"&gt;World&lt;/span&gt; 
  &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You need:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Greeting&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt; 
  &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; 
    Hello World 
  &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When you use curly braces with an arrow function, you are starting the scope of that function. The short syntax of arrow functions does not use curly braces.&lt;/p&gt;




&lt;h4&gt;
  
  
  10 — Not wrapping objects with parentheses
&lt;/h4&gt;

&lt;p&gt;The curly braces vs. parentheses problem above is also confusing when you want to create a short arrow function that returns a plain-old object.&lt;/p&gt;

&lt;p&gt;Instead of:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;myAction&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;DO_THIS&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You need:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;myAction&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;DO_THIS&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Without wrapping the object in parentheses, you would not be using the short syntax. You will actually be defining a label for a string!&lt;/p&gt;

&lt;p&gt;This is common inside the &lt;em&gt;updater&lt;/em&gt; function of the &lt;code&gt;setState&lt;/code&gt; method because it needs to return an object. You need to wrap that object with parenthesis if you want to use the short arrow function syntax.&lt;/p&gt;

&lt;p&gt;Instead of:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;prevState&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;answer&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;42&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You need:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;prevState&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;answer&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;42&lt;/span&gt; &lt;span class="p"&gt;}));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h4&gt;
  
  
  11 — Not using the right capitalization of API elements and props
&lt;/h4&gt;

&lt;p&gt;It’s &lt;code&gt;React.Component&lt;/code&gt;, not &lt;code&gt;React.component&lt;/code&gt;. It’s &lt;code&gt;componentDidMount&lt;/code&gt;, not &lt;code&gt;ComponentDidMount&lt;/code&gt;. It’s &lt;em&gt;usually&lt;/em&gt; &lt;code&gt;ReactDOM&lt;/code&gt;, not &lt;code&gt;ReactDom&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Pay attention to the API capitalization that you need. If you use incorrect capitalization, the errors you’ll get might not clearly state what the problem is.&lt;/p&gt;

&lt;p&gt;When importing from &lt;code&gt;react&lt;/code&gt; and &lt;code&gt;react-dom&lt;/code&gt;, make sure that you’re importing the correct names and what you’re using is exactly the same as what you’re importing. ESLint can help you point out what is not being used.&lt;/p&gt;

&lt;p&gt;This problem is also common in accessing component props:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Greeting&lt;/span&gt; &lt;span class="na"&gt;userName&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"Max"&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;

&lt;span class="c1"&gt;// Inside the component, you need &lt;/span&gt;
&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;userName&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If, instead of &lt;code&gt;props.userName&lt;/code&gt;, you incorrectly use &lt;code&gt;props.username&lt;/code&gt; or &lt;code&gt;props.UserName&lt;/code&gt;, you will be using an undefined value. Pay attention to that, or better yet, make your ESLint configuration point these problems out as well.&lt;/p&gt;




&lt;h4&gt;
  
  
  12— Confusing the state object with instance properties
&lt;/h4&gt;

&lt;p&gt;In a class component, you can define a local &lt;code&gt;state&lt;/code&gt; object and later access it with &lt;code&gt;this&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Greeting&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Component&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
  &lt;span class="nx"&gt;state&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
    &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;World&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
  &lt;span class="p"&gt;};&lt;/span&gt;

  &lt;span class="nf"&gt;render&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;`Hello &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above will output “Hello World”.&lt;/p&gt;

&lt;p&gt;You can also define other local instant properties beside state:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Greeting&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Component&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
  &lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
    &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;World&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
  &lt;span class="p"&gt;};&lt;/span&gt;

  &lt;span class="nf"&gt;render&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;`Hello &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above will also output “Hello World”.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;state&lt;/code&gt; instance property is a special one because React will manage it. You can only change it through &lt;code&gt;setState&lt;/code&gt; and React will &lt;em&gt;react&lt;/em&gt; when you do. However, all other instance properties that you define will have no effect on the rendering algorithm. You can change &lt;code&gt;this.user&lt;/code&gt; in the above example as you wish and React will not trigger a render cycle in React.&lt;/p&gt;




&lt;h4&gt;
  
  
  13 — Confusing &amp;lt;tag/&amp;gt; with &amp;lt;/tag&amp;gt;
&lt;/h4&gt;

&lt;p&gt;Don’t misplace the &lt;code&gt;/&lt;/code&gt; character in your closing tags. Admittedly, sometimes you can use &lt;code&gt;&amp;lt;tag/&amp;gt;&lt;/code&gt; and other times you need &lt;code&gt;&amp;lt;/tag&amp;gt;&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;In HTML, there is something called a “self-closing tag” (AKA void tag). Those are the tags representing elements that do not have any children nodes. For example, the &lt;code&gt;img&lt;/code&gt; tag is a self-closing one:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;img&lt;/span&gt; &lt;span class="na"&gt;src&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"..."&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;

&lt;span class="c1"&gt;// You don't use &amp;lt;img&amp;gt;&amp;lt;/img&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A &lt;code&gt;div&lt;/code&gt; tag can have children, and so you use opening and closing tags:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; 
  Children here...
&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The same applies to React components. If the component has children content, it might look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Greeting&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Hello!&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;Greeting&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;

&lt;span class="c1"&gt;// Notice the position of the / character.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the component does not have children, you can write it with open/closing tags or just a self-closing tag:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// 2 valid ways&lt;/span&gt;

&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Greeting&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;Greeting&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;

&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Greeting&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;

&lt;span class="c1"&gt;// Notice how the / character moves based on whether the element &lt;/span&gt;
&lt;span class="c1"&gt;// is self-closing or not&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The following use is invalid:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Wrong&lt;/span&gt;

&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Greeting&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Greeting&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you misplace the &lt;code&gt;/&lt;/code&gt; character, you will get errors like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Syntax error: Unterminated JSX contents
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h4&gt;
  
  
  14 — Assuming import/export will just work
&lt;/h4&gt;

&lt;p&gt;The import/export feature is an official feature in JavaScript (since 2015). However, it’s the only ES2015 feature that is not yet fully supported in modern browsers and the latest Node.&lt;/p&gt;

&lt;p&gt;The popular configuration of a React project uses Webpack and Babel. Both allow the use of this feature and compile it down to something all browsers can understand. You can only use import/export if you have something like Webpack or Babel in your flow.&lt;/p&gt;

&lt;p&gt;However, having import/export in your React bundled app does not mean that you can just use them anywhere you want! For example, if you’re also doing server-side rendering through the latest Node, things will not work for you. You will most likely get an “&lt;em&gt;unexpected token&lt;/em&gt;” error.&lt;/p&gt;

&lt;p&gt;To have Node understand import/export as well (which is something you need if you use them on the front-end and you want to do SSR as well), you will have to run Node itself with a Babel preset (like the &lt;em&gt;env&lt;/em&gt; preset) that can transpile them. You can use tools like &lt;em&gt;pm2&lt;/em&gt;, &lt;em&gt;nodemon&lt;/em&gt;, and &lt;em&gt;babel-watch&lt;/em&gt; to do so in development and have Node restart every time you change something.&lt;/p&gt;




&lt;h4&gt;
  
  
  15 — Not binding handler methods
&lt;/h4&gt;

&lt;p&gt;&lt;em&gt;I saved this one for last because it’s a big one and a very common problem.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;You can define class methods in a React component and then use them in the component’s &lt;code&gt;render&lt;/code&gt; method. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Greeting&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Component&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
  &lt;span class="nf"&gt;whoIsThis&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;dir&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// "this" is the caller of whoIsThis &lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;World&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nf"&gt;render&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;`Hello &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;whoIsThis&lt;/span&gt;&lt;span class="p"&gt;()}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;ReactDOM&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;render&lt;/span&gt;&lt;span class="p"&gt;(&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Greeting&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;,&lt;/span&gt; &lt;span class="nx"&gt;mountNode&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I used the &lt;code&gt;whoIsThis&lt;/code&gt; method inside the &lt;code&gt;render&lt;/code&gt; method with &lt;code&gt;this.whoIsThis&lt;/code&gt; because inside &lt;code&gt;render&lt;/code&gt;, the &lt;code&gt;this&lt;/code&gt; keyword refers to the component instance associated with the DOM element that represents the component.&lt;/p&gt;

&lt;p&gt;React internally makes sure that “&lt;code&gt;this&lt;/code&gt;” inside its class methods refers to the instance. However, JavaScript does not bind the instance automatically when you use a &lt;em&gt;reference&lt;/em&gt; to the &lt;code&gt;whoIsThis&lt;/code&gt; method.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;console.dir&lt;/code&gt; line in &lt;code&gt;whoIsThis&lt;/code&gt; will correctly report the component instance because that method was called &lt;em&gt;directly&lt;/em&gt; from the &lt;code&gt;render&lt;/code&gt; method with an &lt;em&gt;explicit&lt;/em&gt; caller (&lt;code&gt;this&lt;/code&gt;). You should see the &lt;code&gt;Greeting&lt;/code&gt; object in the console when you execute the code above:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpa1pqwuaer3uu0eo0cr2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpa1pqwuaer3uu0eo0cr2.png" width="800" height="316"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;However, when you use the same method in a &lt;em&gt;delayed-execution&lt;/em&gt; channel, such as an &lt;em&gt;event handler&lt;/em&gt;, the caller will no longer be explicit and the &lt;code&gt;console.dir&lt;/code&gt; line will not report the component instance.&lt;/p&gt;

&lt;p&gt;See the code and output (after clicking) below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Feihhawlraxcxhvy9gwpu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Feihhawlraxcxhvy9gwpu.png" width="800" height="251"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the code above, React invokes the &lt;code&gt;whoIsThis&lt;/code&gt; method when you click on the string, but it will not give you access to the component instance inside of it. This is why you get &lt;code&gt;undefined&lt;/code&gt; when we click the string. This is a problem if your class method needs access to things like &lt;code&gt;this.props&lt;/code&gt; and &lt;code&gt;this.state&lt;/code&gt;. It will simply not work.&lt;/p&gt;

&lt;p&gt;There are many solutions for this problem. You can wrap the method in an inline function or use the &lt;code&gt;.bind&lt;/code&gt; call to force the method to remember its caller. Both are okay for infrequently-updated components. You can also optimize the bind method by doing it in the &lt;em&gt;constructor&lt;/em&gt; of the class instead of in the render method. However, the best solution to this method is to enable the ECMAScript class-fields feature (which is still stage-3) through Babel and just use an arrow function for the handlers:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Greeting&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Component&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
  &lt;span class="nx"&gt;whoIsThis&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;dir&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nf"&gt;render&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
    &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; 
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt; &lt;span class="na"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;whoIsThis&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; 
        Hello World 
      &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; 
    &lt;span class="p"&gt;);&lt;/span&gt; 
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will work as expected:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs11hbasmn5yaq3pot9km.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs11hbasmn5yaq3pot9km.png" width="800" height="239"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That’s all for now. Thanks for reading.&lt;/p&gt;




&lt;p&gt;Check out my &lt;a href="https://jscomplete.com/learn-react-js-by-building-games" rel="noopener noreferrer"&gt;Learn React.js by Building Games&lt;/a&gt; book:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://jscomplete.com/learn-react-js-by-building-games" rel="noopener noreferrer"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fj196pw3wh7y713oa2rix.jpeg" width="203" height="250"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I also have a few other books that might interest you:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://jscomplete.com/learn-coding-with-modern-javascript" rel="noopener noreferrer"&gt;Learn Coding with Modern JavaScript&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://jscomplete.com/learn-node-js-beyond-the-basics" rel="noopener noreferrer"&gt;Node.js Beyond the Basics&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://jscomplete.com/the-professional-programmer" rel="noopener noreferrer"&gt;The Professional Programmer&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;This article was originally published &lt;a href="https://medium.com/@samerbuna/react-js-frequently-faced-problems-45e7060ef884" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>beginners</category>
      <category>javascript</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
