What is React?

0
676
What is React?
What is React?

React is the most popular JavaScript front-end framework in use today. It’s used by both established companies and new startups.

React was released by Facebook in 2013, and they still use it today for many of their applications. If you’re new to React, this article will help introduce you to the basics. Or, if you’d rather learn by doing, you can jump right in with our Learn React course.

What is React used for?

React is a JavaScript library for creating user interfaces. Here are three places you’ll find it being used:

Web development

This is where React got its start and where you’ll find it used most often. React is component-based. An example of a component could be a form or even just a form field or button on a website. In React, you build up complete applications using components like these by nesting them.

Components in React can manage their own state and communicate that state to child components. By “state,” we mean the data that populates the web application.

An example would be a user profile form that holds the state of the user’s data, including first name, last name, and other values. This form component would have nested field components that it passes its state to in order to populate them.

Components in React are also reusable, which means you can use one button component for every button on your site. If you need the button to look different, you can simply change its style.

Mobile app development

React Native is a JavaScript framework that uses React. With React Native, developers can apply web-based React principles to creating mobile apps for Android and iOS. Here, React is used to connect the mobile user interface of the application to the phone’s operating system.

Desktop app development

Developers can also use React with Electron, another JavaScript library, to create cross-platform desktop apps. Some apps you may know about that are built with Electron include Visual Studio Code, Slack, Skype, Discord, WhatsApp, and WordPress Desktop.

React example

Let’s look at a React example so you have a better idea of how it works. A React web application runs on one web page. Here’s an example of an HTML file used to run a React app:

       

Using script tags, we import the JavaScript libraries necessary for React to run. React and React DOM are the basic React libraries we need. Babel is a JavaScript library that compiles the JSX language that React uses into JavaScript the browser can understand.

The div element that has the root id is where the whole React app will run. And the script tag below that is where all the React code will go, which we’ll look at in more detail next.

To use React, you need to create components that use JSX. Below is an example component:

function Heading(props) { return 

{props.value}

; }

The component above is a simple JavaScript function that accepts a parameter called props — a special keyword in React used to pass data between components.

The function returns the value attribute of props wrapped with what looks like an HTML h1 tag. This isn’t actual HTML, yet. It’s called JSX, a React-specific syntax that allows HTML-like text to co-exist with JavaScript.

Below, we have another component that uses our Heading component. In fact, it uses it three times to create headings with different values. Note that the props in React are passed to child components as attributes of the JSX.

function App() { return ( 
); }

You’ll also notice that the name of this component is App. This is because it’s our complete application. Everything in React is a component, including the whole application. To render this app to the browser, we need one more piece of code.

ReactDOM.render( , document.getElementById('root') );

This code tells React to render the results in the HTML element that has the root id.

React vs. Angular

React is often compared to another JavaScript framework called Angular, but there are a lot of differences between the two. Here’s an overview of some of the main distinctions between the two frameworks:

  • Web development: Angular is a complete front-end framework for building web applications. It’s based on the MVC (Model-View-Controller) pattern. React, on the other hand, is for creating user interfaces — or just the View part of this pattern. This means you’ll need other libraries or more code to create complete web apps, but you’ll also have greater customizability for your features and structure.
  • Updates: Angular operates directly on a web page’s DOM, so the entire page must be updated when a change is made to its data model. React updates the virtual DOM. These updates are quicker because they don’t trigger changes in the page layout itself. Plus, once React updates the virtual DOM, it’s then compared to the real DOM, and only those parts that differ are updated — allowing for faster rendering and performance.
  • Data binding: Angular uses both one- and two-way data binding, while React only uses one-way. With Angular, changes in data can trigger changes in the view and vice versa. But, with React, data only flows in one direction — which makes debugging an app easier.

How to learn more about React

So, now you know a little about React and why developers use it to build applications. If you’re ready to learn more about React and start building front-end web applications, our Learn React course will teach you the framework’s fundamentals and essential concepts. To apply these newly learned skills and build a complete front-end web app, check out Create a front-end app with React.


Mobile Development Courses & Tutorials | Codecademy

Every year more and more people rely on mobile devices to meet their needs. Where websites used to be the gold-standard, people now rely on mobile apps. The technologies used to create these apps are expanding and improving quickly, so it’s an exciting time to start learning Mobile Development!

Web Development Courses & Tutorials | Codecademy

Web Development is the practice of developing websites and web apps that live on the internet. Whether you’re interested in front-end, back-end, or going full-stack, the content in our Web Development domain will help you get there.