What Is React?
React is a JavaScript library for building user interfaces. It helps developers create interactive web applications by dividing the interface into reusable components. Instead of manually updating individual DOM elements whenever data changes, developers describe what the interface should look like for a particular state, and React coordinates the necessary updates.
React is commonly used for single-page applications, dashboards, e-commerce websites, social platforms, and complex business applications. It can also be integrated into existing websites or used with frameworks that support server rendering and other application features.
React focuses on the user interface rather than providing every feature required by a complete application. Routing, server communication, form management, and other capabilities can be added through browser APIs, React APIs, or compatible libraries.
Why Use React?
Building an interactive website with plain JavaScript often involves selecting DOM elements, updating their content, tracking application data, and keeping multiple interface sections synchronized. As an application grows, managing these updates manually can become difficult.
React provides a declarative approach. You describe the interface using components and data, and React manages rendering and updates. This makes complex interfaces easier to organize, test, and maintain when components have clear responsibilities.
- Reusable components: Build interface elements once and use them in multiple locations.
- Declarative rendering: Describe the desired interface instead of manually managing every DOM update.
- State management: Represent changing data and update the interface when that data changes.
- One-way data flow: Pass information from parent components to child components through props.
- Rich ecosystem: Use compatible tools for routing, testing, data fetching, and development.
- Flexible integration: Introduce React into an existing application or use it as the foundation for a new one.
How React Works
React applications are built from components. A component receives inputs, such as props, and can maintain its own state. It returns a description of the interface, usually written with JSX. React uses that description to determine what needs to be rendered or updated in the browser.
A simplified React rendering flow
- A component receives props or reads its state.
- React executes the component to calculate its UI description.
- When relevant data changes, React renders the component again.
- React reconciles the new description with the previous one.
- React commits the necessary changes to the DOM.
A render does not necessarily mean React replaces the entire page. React compares the new result with the previous result and commits the changes required by the update. The browser then displays the resulting interface.
Your First React Component
A React component is a reusable unit of UI. Modern React applications commonly define components as JavaScript functions that return JSX. Component names must begin with an uppercase letter so React can distinguish components from built-in HTML elements.
function Welcome() {
return (
<section>
<h1>Welcome to React</h1>
<p>Build interactive interfaces with reusable components.</p>
</section>
);
}
export default function App() {
return (
<main>
<Welcome />
</main>
);
}
The Welcome component defines a section of the interface, and the App component renders it. The expression <Welcome /> tells React to render the component rather than create an HTML element named Welcome.
Learn more in the React components guide.
Understanding JSX
JSX is a syntax extension commonly used with React to describe UI using markup-like expressions inside JavaScript. It allows developers to combine interface structure with JavaScript expressions while keeping related rendering logic together.
const userName = 'Alex';
const unreadMessages = 3;
function Notification() {
return (
<div>
<h2>Hello, {userName}</h2>
<p>You have {unreadMessages} unread messages.</p>
</div>
);
}
JavaScript expressions can be inserted inside curly braces. JSX also uses conventions such as className for CSS classes, camelCase names for many DOM properties, and closing tags for elements. JSX is transformed into JavaScript by the build tooling.
Explore the syntax in the React JSX tutorial.
Passing Data with Props
Props are inputs passed from a parent component to a child component. They allow the same component to display different content without duplicating its implementation. A child component should treat its props as read-only and ask its parent to make changes when necessary.
function UserCard({ name, role }) {
return (
<article>
<h2>{name}</h2>
<p>Role: {role}</p>
</article>
);
}
export default function App() {
return (
<main>
<UserCard name="Alex" role="Developer" />
<UserCard name="Sam" role="Designer" />
</main>
);
}
Both cards use the same component but receive different values. This approach is useful for product cards, navigation items, profile panels, and other repeated interface elements.
Continue with the React props guide.
Managing Changes with State
Props describe data received from a parent, while state represents data a component manages over time. When state changes, React can render the component again to reflect the updated value. The useState Hook is the standard way to add local state to a function component.
import { useState } from 'react';
export default function Counter() {
const [count, setCount] = useState(0);
function increment() {
setCount((currentCount) => currentCount + 1);
}
return (
<section>
<h2>Counter</h2>
<p>Current count: {count}</p>
<button onClick={increment}>
Increment
</button>
</section>
);
}
Each click requests a state update, and React updates the displayed count. The functional updater receives the previous state value, making it useful when the next value depends on the current one.
Read the React state tutorial to understand state updates and immutable data changes.
Handling User Events
React supports event handlers for interactions such as clicks, typing, form submissions, and keyboard input. Event handlers are passed as functions through props like onClick and onChange.
import { useState } from 'react';
export default function GreetingForm() {
const [name, setName] = useState('');
return (
<form onSubmit={(event) => {
event.preventDefault();
alert('Hello, ' + (name || 'visitor') + '!');
}}>
<label htmlFor="name">Your name</label>
<input
id="name"
value={name}
onChange={(event) => setName(event.target.value)}
/>
<button type="submit">Greet me</button>
</form>
);
}
The input is controlled by React state, and the form handler prevents the browser's default page submission. This pattern is commonly used for search boxes, login forms, and data entry interfaces.
Learn more in the React events guide and React forms tutorial.
What Are React Hooks?
Hooks are functions that let function components use React features such as state, effects, context, and references. Common built-in Hooks include useState, useEffect, useContext, and useRef.
Hooks must follow the Rules of Hooks: call them at the top level of a function component or a custom Hook, not inside conditions, loops, or nested functions. This lets React associate Hook calls with the correct component state.
import { useEffect, useState } from 'react';
export default function PageTitle() {
const [title, setTitle] = useState('React Basics');
useEffect(() => {
document.title = title;
}, [title]);
return (
<label>
Page title
<input
value={title}
onChange={(event) => setTitle(event.target.value)}
/>
</label>
);
}
This example synchronizes the browser document title with React state. Effects are intended for synchronizing with external systems; they are not necessary for every calculation or state update.
Explore the React Hooks guide and the useEffect tutorial.
React Compared with Traditional DOM Manipulation
| Aspect | Traditional DOM approach | React approach |
|---|---|---|
| UI updates | Often performed through explicit DOM operations. | Describe UI based on state and props. |
| Organization | Can require coordinating DOM code across the application. | Organize UI into reusable components. |
| Data changes | Application code must keep the DOM synchronized with data. | State and props determine rendered output. |
| Rendering model | Directly manipulate selected elements. | React reconciles UI descriptions and commits required changes. |
React does not eliminate the DOM or make direct DOM access impossible. Instead, it provides a structured model for describing and updating interfaces. Direct DOM access is still appropriate for certain integrations, measurements, and browser APIs.
Setting Up a React Project
A practical way to begin learning React is to create a project with a modern development tool. Vite provides a lightweight development server and build pipeline for React applications.
npm create vite@latest my-react-app -- --template react
cd my-react-app
npm install
npm run dev
These commands create a React project, install dependencies, and start the local development server. Follow the terminal output to open the application in your browser. Use a currently supported Node.js version and consult the tooling documentation if setup options change.
Follow the complete React setup guide for project structure and development workflow.
Where React Fits in a Web Application
React is responsible primarily for building user interfaces. A production application may also need routing, API communication, authentication, data validation, styling, testing, and deployment infrastructure.
- Routing: Map URLs to views with a routing library or framework.
- Data fetching: Retrieve information from APIs and manage loading and error states.
- Styling: Use CSS, CSS Modules, utility classes, or a component library.
- Testing: Verify component behavior and user interactions.
- Performance: Use measurement, code splitting, and appropriate rendering patterns.
- Deployment: Build and host the application using an appropriate production platform.
Explore API fetching, React styling, performance optimization, and deployment as you progress.
Best Practices for Beginners
- Build small components with clear and focused responsibilities.
- Use props to pass information and callbacks between related components.
- Use state for values that change over time and affect the rendered interface.
- Keep state updates immutable, especially when updating objects and arrays.
- Use stable, unique keys when rendering lists of elements.
- Call Hooks at the top level of function components or custom Hooks.
- Use semantic HTML and accessible labels for interactive controls.
- Avoid unnecessary effects when a value can be calculated directly during rendering.
- Separate UI responsibilities and data-access logic as the application grows.
- Learn to use browser developer tools and React Developer Tools to inspect behavior and performance.
Conclusion
React provides a component-based, declarative approach to building interactive web interfaces. Its core concepts include JSX, components, props, state, event handlers, and Hooks. Understanding these fundamentals makes it easier to build maintainable applications and learn advanced topics such as routing, data fetching, performance optimization, and server rendering.
Continue your learning journey with JSX, components, props, and state management.