In a Single-Page Application (SPA) — a web application that runs on a single HTML page — a Component-Based Architecture is essential for organizing different sections and layouts efficiently.
React enables this by providing a declarative, reusable, and scalable component model that simplifies UI development.
[!info] npm Commands
npm init— initialize a new Node.js project
npm i— install dependencies
npm i <package>— install a specific package
npm run <script>— run a script from package.json
Create React App (CRA)
npx create-react-app <appName>
Slower because it includes full bundling and tooling.
Vite
npm create vite@latest <appName>
Faster — uses a modern build tool with optimized dev server.
| File | Purpose |
|---|---|
| index.html | The single HTML page of your SPA |
| package.json | Project metadata, dependencies, and scripts |
| vite.config.js | Optional Vite build configuration |
| /src | Main source folder for React code |
| main.jsx | Entry point, connects React to HTML |
| App.jsx | Root React component |
| App.css | CSS specific to App.jsx |
| index.css | Global CSS for the entire app |
index.html
↓
main.jsx → connects React to the HTML page
↓
<App.jsx> → root component rendered inside #root
↓
App.css → component-specific styles
index.css → global styles
<> </> fragments for grouping{} for dynamic expressionsclassName, onClick, onChangeReact uses a Virtual DOM and a process called Reconciliation.
[!info] Features
- Must include a
render()method- Can maintain state
- Can receive props
class MyComponent extends React.Component {
constructor(props) {
super(props);
this.state = { count: 0 };
}
render() {
return <h1>Hello, {this.props.name}</h1>;
}
}
Props are data passed from a parent component to a child component.
this.props.propNameprops as argumentsuper(props)const { name, age } = this.props;| Method | Example | Notes |
|---|---|---|
| Constructor binding | this.handleClick = this.handleClick.bind(this) |
Best performance |
| Arrow method | handleClick = () => {...} |
Easiest to write |
| Inline arrow | onClick={() => this.handleClick()} |
Acceptable for small cases |
State stores component-specific data that changes over time.
this.state = { value: 0 }; // initialization
this.setState({ value: 1 }); // update
this.setState(prev => ({ value: prev.value + 1 })); // functional update
Still in Production
React class components move through three major phases:
constructor() → render() → componentDidMount() →
componentDidUpdate() → componentWillUnmount()