--- title: React --- # React React is a JavaScript library for building user interfaces. It is maintained by [Facebook](https://github.com/facebook/react) and a vast community of individual developers and companies. It was voted the most loved in the "Frameworks, Libraries, and Other Technologies" category of Stack Overflow's 2017 Developer Survey.1 React is used for building user interfaces - what the user sees on their screen and how they interact with your web app. This interface is split up into components. Instead of having one huge page, you break it up into smaller pieces known as components. This approach is called modularity. - It's declarative: React uses a declarative paradigm that makes it more readable. - It's efficient: React computes the minimal set of changes necessary to keep your DOM up-to-date. - It's flexible: React allows the user to render one or many components to the browser. - And it's compatible: React works well with many popular libraries and frameworks. ## Why learn React? 1. React involves composition: lots of components wrapping up the functionalities into an encapsulated container. Many popular websites use React implementing the Model-View-Controller (MVC) architectural pattern. Facebook (partially), Instagram (completely), Khan Academy (partially), Codecademy (partially), New York Times (partially), Yahoo Mail (completely), Dropbox's new photo and video gallery app carousel (completely) are some popular examples using React. How are these large applications built using React? The simple answer is by building small applications or components. Example: ```jsx const Component2 = () => { return (
); }; const Component3 = () => { return (
); }; const Component1 = () => { return (
); }; ReactDOM.render( , document.getElementById("app") ); ``` 2. React is declarative, which means you are concerned more with **what** to do rather than **how** to do a specific task. Declarative programming is a programming paradigm that expresses the logic of a computation without describing its control flow. Declarative programming comes with certain advantages such as reduced side effects that occur when you modify any state, mutate something, or make an API request. Other advantages are minimizing mutability (mostly abstracted), enhanced readability, and fewer bugs. 3. Unidirectional dataflow. UI in React is actually the function of the state, which means that as the state updates, it updates the UI as well. So our UI progresses as the state changes. ## Advantages of React Some reasons to use React are: 1. It's Fast. Apps made in React can handle complex updates and still feel quick and responsive. 2. It's Modular. Instead of writing large, dense files of code, you can write many smaller, reusable files. React's modularity can be a beautiful solution to JavaScript's [maintainability problems](https://en.wikipedia.org/wiki/Spaghetti_code). 3. It's Scalable. Large programs that display a lot of changing data are where React performs best. 4. It's Flexible. You can use React for interesting projects that have nothing to do with making a web app. People are still figuring out React's potential. [There's room to explore](https://medium.mybridge.co/22-amazing-open-source-react-projects-cb8230ec719f). ### Virtual DOM React's magic comes from its interpretation of the DOM and its strategy for creating UIs. React uses the virtual DOM to render an HTML tree virtually first. 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 Tree Reconciliation. ### Reconciliation React has a smart diffing algorithm that it uses to only regenerate in its DOM node that needs to be regenerated, while it keeps everything else as is. This diffing process is possible because of React’s virtual DOM. 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. 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 UI, React will take to the browser only the new “partial” updates. ## React from Scratch Would you like to get started learning the basics of React without getting bogged down creating a development environment? Chances are that if you are new to web development, just setting up a development environment can leave you feeling a little intimidated. In this article, you will start learning React using only a text editor and a browser. Watch Video Here ### 1 — Set Up Boiler Plate Code with Emmet Let’s get started with step 1. You’ll begin with a file in our browser called “index.html”. You’ll begin with the boilerplate HTML code. For a quick start I recommend using Emmet with whatever text editor you have, and on the first line typing in `html:5` then pressing the shift key to get the code below. Or you can go ahead and copy and paste the code from below. ```javascript html:5 ``` This will result in the following code: ```javascript Document ``` You can fill in the title as “Time to React!”. This content will not appear on your webpage. Anything in the head section of the HTML file will be metadata that our browser will use to interpret the code in the body section. This title is going to be what appears on the tab for your page, not actually on the page. ### 2 - Get Script Tags to Harness the Power of React and Babel Libraries Ok, item one is checked off your list. Let’s look at item two. You are going to set up our developer environment by using script tags to bring in React and Babel. This is not a real-life developer environment. That would be quite an elaborate setup. It would also leave us with a lot of boilerplate code and libraries that would take us off the subject of learning React basics. The goal of this series is to go over the basic syntax of React and get right into coding. You are going to use ` ... Time to React! ``` You are free to use more updated versions of these libraries as they come out. They should not create any breaking changes for the content you are covering. What are we doing here? The: HTML ` ``` The “type” of the script that you are using is wrapped in quotes and set it to `"text/babel"`. You’ll need this ability to use Babel right away as you work with JSX. First, you are going to render React to the DOM. You will use the `ReactDOM.render()` method to do this. This will be a method, and remember a method is just a function attached to an object. This method will take two arguments. ```javascript
React has not rendered yet
``` The first argument is the “what” of React. The second argument is the “where” of the location you want it to be placed in the DOM. Let’s start by calling our ReactDOM.render() method. Your first argument is going to be your JSX code. ```javascript
React has not rendered yet
``` The [official React docs state](https://reactjs.org/docs/introducing-jsx.html): “This funny tag syntax is neither a string nor HTML. It is called JSX, and it is a syntax extension to JavaScript. You recommend using it with React to describe what the UI should look like. JSX may remind you of a template language, but it comes with the full power of JavaScript. JSX produces React “elements”.” Often times, JSX freaks out people who have been developers for a while because it looks like HTML. Developers have traditionally been taught early on about separation of concerns. HTML has its place, CSS has its place and JavaScript has its place. JSX seems to blur the lines. You are using what looks like HTML but as React says, has the full power of JavaScript. This can alarm some people, so many React tutorials start without JSX, which can complicate getting started. You won’t do that. Because this course is directed towards those who are very early in their careers, you may not be as alarmed by the syntax. And JSX is just really intuitive. You can probably quite easily read this code and see that this is going to be the largest header tag displaying the text “Hello World”: no mystery and pretty straightforward. Now, let’s look at what your second argument would be. ```javascript
React has not rendered yet
``` This is where you want your React content rendered to the DOM. You’ve probably done this quite a few times in the past. You’ll just type in `document.getElementById()`. And you’ll pass into the argument of the id of the app. And that is it. You will now target the `
` with the id of the app to insert our react to content. You want to make sure your content is saved. Go ahead and open this up in your browser, and you should see “Hello World”. As you can probably guess, using React is not the quickest or best way to create a Hello World app. You aren’t quite seeing the benefits of it yet. But now, you know that everything is working. Go ahead and open up the console and look at the “Elements” tab. You can do that on a mac with command + shift + j or on an On Windows and Linux: Ctrl + Shift + J If you click on the head tag you can see the script libraries we included. Then you can go down to the body of our document. Let’s click on your div with the id of `app`, and see our `

` tag with the content “Hello World”. [View Entire Code Here](https://github.com/robgmerrill/hello-react/blob/master/section-one/index.html) or Watch Video Here ### Quick React App Setup Setting up a react app can be time-consuming. A great tool for starting a new react project is create-react-app. Get more information about it [here](https://github.com/facebook/create-react-app#readme) ### Recap So let’s do a quick recap. In your head tag you grabbed the script tags for React, ReactDOM and Babel. These are the tools your browser needs in its metadata to read your React code. You then located the position within the DOM that you wanted to insert your React by creating an element `
` with the id of “app”. Next, you created a script tag to input your React code. You used ReactDOM.render() method that takes two arguments. The “what” of the React content, in this case your JSX, and the second argument is the “where” that you want to insert the React content into the DOM. In this case it is the location with the id of “app”. As an alternative to JSX, you can use ES6 and Javascript's compiler like Babel. [https://babeljs.io/](https://babeljs.io/) ### More Information: - [React Homepage](https://reactjs.org/) - [Dan Abramov's Twitter](https://twitter.com/dan_abramov) - [React Tutorials at Egghead.io](https://egghead.io/browse/frameworks/react) ### Sources 1. ["Developer Survey Results 2017."](https://insights.stackoverflow.com/survey/2017#technology-most-loved-dreaded-and-wanted-frameworks-libraries-and-other-technologies) Stack Overflow. Accessed: October 28, 2017.