React.js Overview
React.js Overview.
Introduction to React.js
React.js, commonly referred to as React, is an open-source JavaScript library for building user interfaces. It was developed by Facebook and is now maintained by Facebook and a community of individual developers and companies.
React is known for its component-based architecture, which allows developers to build reusable UI components. These components are then combined to create complex user interfaces. React follows a declarative approach, where developers describe what the UI should look like, and React takes care of updating the UI when the underlying data changes.
History of React.js
React was first created by Jordan Walke, a software engineer at Facebook, in 2011 to address the need for a more efficient way to update the user interfaces of Facebook's web applications. It was later open-sourced in 2013.
Since its release, React has gained immense popularity and has become one of the most widely used JavaScript libraries for building user interfaces. It has a large and active community, which contributes to its ongoing development and improvement.
Features of React.js
1. Virtual DOM
React uses a virtual DOM (Document Object Model), which is a lightweight copy of the actual DOM. The virtual DOM allows React to efficiently update only the parts of the UI that have changed, instead of re-rendering the entire UI. This results in faster performance and a smoother user experience.
Here's an example of how React's virtual DOM works:
// Initial render
const element = <h1>Hello, React!</h1>;
ReactDOM.render(element, document.getElementById('root'));
// Update the DOM
const updatedElement = <h1>Hello, React!</h1>;
ReactDOM.render(updatedElement, document.getElementById('root'));
In the above code, React compares the updated element with the initial render and only updates the necessary changes in the actual DOM.
2. Component-Based Architecture
React follows a component-based architecture, where UIs are built by combining reusable components. Each component encapsulates its own logic and state, making it easy to manage and test.
Here's an example of a simple React component:
class Button extends React.Component {
render() {
return <button>{this.props.label}</button>;
}
}
ReactDOM.render(<Button label="Click me!" />, document.getElementById('root'));
In this code, the Button component is defined and rendered with a prop label. The render method returns a button element with the label passed as a prop.
3. JSX Syntax
React uses JSX (JavaScript XML), an extension to JavaScript that allows you to write HTML-like code within JavaScript. JSX makes it easier to define components and their structure.
Here's an example of JSX syntax:
class Message extends React.Component {
render() {
return <div>
<h2>{this.props.title}</h2>
<p>{this.props.content}</p>
</div>;
}
}
ReactDOM.render(
<Message title="Welcome" content="Hello, React!" />,
document.getElementById('root')
);
In this code, the Message component is defined using JSX syntax, with a title and content prop. The render method returns a div element containing an h2 and p element.
Examples of React.js
Example 1: Counter
Let's start with a simple counter component that increments a value when a button is clicked.
class Counter extends React.Component {
constructor(props) {
super(props);
this.state = { count: 0 };
}
incrementCount() {
this.setState(prevState => ({ count: prevState.count + 1 }));
}
render() {
return (
<div>
<p>Count: {this.state.count}</p>
<button onClick={() => this.incrementCount()}>Increment</button>
</div>
);
}
}
ReactDOM.render(<Counter />, document.getElementById('root'));
In this code, the Counter component maintains a state count and updates it when the button is clicked. The incrementCount method uses setState to update the state and trigger a re-render of the component.
Example 2: Todo List
Let's build a simple todo list where users can add and remove items.
class TodoList extends React.Component {
constructor(props) {
super(props);
this.state = { todos: [], newTodo: '' };
}
addTodo() {
this.setState(prevState => ({
todos: [...prevState.todos, prevState.newTodo],
newTodo: ''
}));
}
removeTodo(index) {
this.setState(prevState => ({
todos: prevState.todos.filter((_, i) => i !== index)
}));
}
render() {
return (
<div>
<input
type="text"
value={this.state.newTodo}
onChange={event => this.setState({ newTodo: event.target.value })}
/>
<button onClick={() => this.addTodo()}>Add</button>
<ul>
{this.state.todos.map((todo, index) => (
<li key={index}>
{todo}
<button onClick={() => this.removeTodo(index)}>Remove</button>
</li>
))}
</ul>
</div>
);
}
}
ReactDOM.render(<TodoList />, document.getElementById('root'));
In this code, the TodoList component maintains a state todos which is an array of todo items, and newTodo which stores the new todo item entered by the user. The addTodo method adds the new todo item to the todos array, and the removeTodo method removes a todo item from the array.
To learn more about React, you can visit the official website.
I hope this tutorial gives you a good understanding of React.js and its features.