React has solidified its position as the go-to JavaScript front-end framework in the current tech landscape. It’s fascinating to see how it’s seamlessly woven itself into the development practices of well-established corporations and budding startups alike.
Originally unveiled by Facebook in 2013, React has since evolved into a cornerstone technology, forming the backbone of numerous applications, including those powering the very platform it originated from. If you’re new to React, this article will help introduce you to the basics. Whether you’re curious about the fundamental concepts or eager to dive straight into hands-on learning, our Learn React course provides an immersive and practical experience.
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.
Learn something new for free
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 <h1>{props.value}</h1>; }
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 ( <div> <Heading value="Welcome" /> <Heading value="To" /> <Heading value="React" /> </div> ); }
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( <App />, 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.