React Architecture
Understand how React applications are structured, how components work together, and how data flows from the application to the user interface.
1. What Is React Architecture?
React architecture describes how a React application is organized into components and how those components work together to build the user interface.
A typical React application has an entry point, a root component, reusable child components, data passed through props, and state that manages changing information.
2. React Architecture Diagram
Follow the flow from the application entry point to the components rendered on the screen.
main.jsx
Starts the React application
App.jsx
Defines the main application UI
Header
Navigation area
CourseList
Displays courses
Footer
Bottom information
Browser UI
What the user sees on the screen
3. Main Parts of React Architecture
Entry Point
The entry point is usually main.jsx.
It connects the React application to an HTML element
and renders the root component.
Root Component
The App.jsx component is commonly used as
the root component. It organizes the main application
layout and child components.
Reusable Components
Components such as Header, Footer and CourseCard separate the interface into small, manageable parts.
Props and State
Props pass information to child components. State stores information that can change over time and cause the UI to update.
4. Example Project Structure
A simple React project created using Vite may contain files organized like this:
5. Example: main.jsx
The main.jsx file finds the HTML element with the
ID root and renders the App component
inside it.
import { StrictMode } from 'react';
import { createRoot } from 'react-dom/client';
import App from './App.jsx';
createRoot(document.getElementById('root')).render(
<StrictMode>
<App />
</StrictMode>
);
Code Explanation
StrictModehelps identify potential problems during development.createRoot()creates a React root for the HTML element.Appis the root component rendered by React.render()displays the component in the root element.
6. Example: App.jsx with Child Components
In this example, App uses separate components
for the header, course list and footer.
function Header() {
return <h2>CIIT Training Institute</h2>;
}
function CourseList() {
return (
<div>
<p>Java Full Stack</p>
<p>Python Full Stack</p>
<p>React Development</p>
</div>
);
}
function Footer() {
return <p>Learn. Practice. Grow.</p>;
}
function App() {
return (
<div>
<Header />
<CourseList />
<Footer />
</div>
);
}
export default App;
CIIT Training Institute
Java Full Stack
Python Full Stack
React Development
Learn. Practice. Grow.
7. How Data Flows in React
React generally follows one-way data flow. A parent component can pass information to a child component through props. A child can notify a parent about an event using a callback function passed through props.
Parent Component
Owns the data
Child Component
Receives and displays data
Parent Handles Event
Can update its state
Simple Props Example
function CourseCard({ name }) {
return <h3>Course: {name}</h3>;
}
function App() {
return (
<div>
<CourseCard name="React Development" />
</div>
);
}
export default App;
Course: React Development
8. How React Updates the UI
When a component's state changes, React can render the component again. React compares the newly calculated UI with its previous representation and applies the necessary updates to the DOM.
User Action
The user clicks a button.
State Update
The component updates its state.
Re-render
React runs the component again.
UI Update
The required DOM changes are applied.
9. Benefits of Component-Based Architecture
| Benefit | Explanation |
|---|---|
| Reusability | The same component can be used in multiple places. |
| Maintainability | Smaller components are easier to understand and update. |
| Separation of Concerns | Each component can manage a specific part of the UI. |
| Testability | Individual components can be tested in isolation. |
| Scalability | A clear structure helps organize growing applications. |
Summary
- React applications are organized using components.
main.jsxcommonly provides the entry point.App.jsxis commonly used as the root component.- Props pass information from parent components to child components.
- State manages changing data and can trigger re-renders.
- React updates the DOM as needed when the rendered UI changes.
- Component-based architecture improves reuse and maintainability.