Learning Resources for Software Engineering Students ยป
Author: Aadyaa Maddi
Reviewers: Amrut Prabhu, Marvin Chin
The official website describes React as follows:
React is a declarative, efficient, and flexible JavaScript library for building user interfaces. It lets you compose complex UIs from small and isolated pieces of code called 'components'.
Let us understand some key features of React with the help of an example. A web application that displays the name of a person is given below:
The UI of a React application is defined using a mix of HTML code and HTML-like syntax, called JSX. The main view of the application above (defined in the App.render()
method in index.js
) is given as follows:
<div className="App">
<h1>Person Data</h1>
<PersonComponent name="{this.state.name}" changeHandler="{this.handleChange}"/>
</div>
In the above example, we declare what we want to display (i.e. name
) and React renders the appropriate view based on the updated application data. This is known as the
React encapsulates application views and relevant data and logic for updating the views using components. A combination of components that exchange information with one another is used to build the UI of a React application.
For example, the application above is divided into two components:
App
The App
component (defined in index.js
) contains the main view of the application. It stores the application data (i.e. name
) in an object called state
and has a method to update
the it every time the value in the textbox changes. It passes the application data and the method to the PersonComponent
.
class App extends Component {
state = {
name: "John Doe"
};
handleChange = event => {
this.setState({
name: event.target.value
});
};
render() {
return (
<div className="App">
<h1>Person Data</h1>
<PersonComponent name="{this.state.name}" changeHandler="{this.handleChange}"/>
</div>
);
}
}
PersonComponent
PersonComponent
(defined in personComponent.js
) renders the details of the person. It accepts input from the App
component in the form of props
.
const PersonComponent = props => {
return (
<div className="Person">
<h2>About Me</h2>
<p>My name is {props.name}.</p>
<label htmlFor="name">Name: </label>
<input id="name" onChange="{props.changeHandler}" value="{props.name}"/>
</div>
);
};
As you can see, React components are just JavaScript functions that accept arbitrary input and return a declarative view describing what should appear in the application's UI.
Data binding refers to the exchange of information between the application data and the UI. React provides one-way data binding. In applications that use one-way data binding, changes to the application data are automatically reflected in the UI. However, changes to the UI need to be manually propagated to the application data.
In the above application, you can see that the UI is updated whenever a different name is entered in the textbox. These updates do not happen in a single step - the application data is first updated using the handleChange()
method,
and then the UI is updated to reflect these changes.
Now that we know what React is, let us look at some benefits it has to offer.
Web applications can have a lot of user interaction and data updates, which results in changes being made to the
React minimizes this update time by using a virtual DOM. The virtual DOM is a JavaScript object that is kept in the memory of your application.
Figure 1. How React's actual DOM gets updated.
As shown in Figure 1 above, updates to the UI will first be made to the virtual DOM. Then, React will compare the virtual DOM with the actual DOM using a diffing algorithm. Finally, React updates the actual DOM only in places it differs with the virtual DOM. It batches multiple changes together and updates the actual DOM in one go, minimizing update time.
The traditional
Most web applications usually have to interact with a lot of DOM elements and events. Also, different browsers have variations in their implementations of the
React's declarative approach simplifies this process because it abstracts the complexity of interacting with the actual DOM elements and events. For example, the virtual DOM helps React abstract browser-specific operations on DOM elements. Additionally, React provides its own events system so that events can work in the same way across different browsers.
A React application is made up of a combination of components. Components are independent from each other, and like functions, they map the same input to the same output. This makes it easy to write unit tests for your application.
Additionally, React only allows data to flow downwards (one-way data binding) using state
and props
, which makes your application easier to debug as you can be sure that the data updates the UI, and never the other way around.
Besides the three main benefits explained above, React has the following advantages:
Like any other framework/library, React has its share of disadvantages.
There are a lot of JavaScript frameworks and libraries that you can use to build your next web application. Some popular alternatives to React are Angular and Vue.
How do you decide which one to use? Here are some resources to help you choose between them:
Every framework has its pros and cons, but hopefully you have managed to see that React removes some of the complexity that comes with building user interfaces.
The official React website is a great place to get started. It includes:
If you want to add React to an existing project, you can take a look at React's official guide for doing so. Alternatively, if you are creating a new React application, you can use one of the recommended toolchains to get the best user and developer experience.
Create React App is a convenient environment for learning React, and it is the recommended way to create
The official website also has advanced guides if you want to understand how React works behind the scenes.
As React is a fairly popular library, you can find a lot of comprehensive resources online. Here are some resources that can be useful:
If you need help with React, you can get support from React's community of millions of developers that are active on Stack Overflow and discussion forums like Dev and Hashnode.
Lastly, if you want to know what to learn after getting familiar with React, here is a comprehensive roadmap that you can follow to become a full-fledged React developer.