-
๐ Reusable Components: In React, you can create reusable components, like a navbar, and use it on every page of your application.
-
โก State Management: React allows you to use states, so when you update a variable, it reflects across the page dynamically.
-
๐งฉ Component-Based Architecture: Split your app into multiple components and reuse them wherever needed.
- Why use
className
instead ofclass
?
Since React uses JavaScript to create applications, if you useclass
, React will interpret it as an Object-Oriented Programming (OOP) class instead of a CSS selector. Therefore, we useclassName
for styling.
- What is
useState
?
A special variable in React that can be updated dynamically, and these updates are reflected in the DOM.
- Why use curly braces
{ }
?
In React, curly braces allow you to include JavaScript expressions within your HTML (JSX). Without them, React will treat the content as plain HTML.
- The
App.js
file is the main component of your React application. - It is the entry point that renders when you run your app.
- A component in React is anything that appears on your application.
- Examples:
- A navbar
- A card
- A footer
- Props allow you to pass data from one component to another.
- They enable communication between components and make them more dynamic.
A React App is a project built using React, a popular JavaScript library for building user interfaces.
- When you create a React app (using tools like Create React App or Vite), it provides a ready-to-use folder structure and essential configurations for development. ๐
- Vite is a fast and modern build tool that provides a smoother development experience for React projects. ๐
- It is much faster than Create React App (CRA) and optimized for modern web development. ๐ฅ
-
If you're using npm to run your React project and encounter issues (e.g.,
npm run dev
throws an error), it likely means npm is not installed properly. โ -
To fix this, you can install dependencies and start the project using these commands:
npm i # Shortcut for npm install npm run dev
-
๐ Key Notes:
-
npm i: Installs all the required dependencies. ๐ฆ
-
npm run dev: Starts your development server. ๐
-
React components are function-based.
-
JSX
: JSX is like HTML but acts as an entry point for React, similar toindex.html
in traditional HTML.
-
JSX is like HTML with JavaScript, but it is more strict. For example:
<!-- In HTML --> <img src="home.jpg"> <!-- No error --> <!-- In JSX --> <img src="home.jpg"> <!-- This will throw an error --> <img src="{home.jpg}" /> <!-- Correct way: self-closing tag -->
-
Proper Tag Closing:
Always close tags correctly. For self-closing tags, ensure they are properly closed:-
โ
<img />
-
โ
<img>
-
-
Wrapper for Content:
All component content must be wrapped in a single parent element, like:<div>
or<>
(React Fragment).
-
Use
className
:
UseclassName=""
instead ofclass
to add CSS classes to elements. -
Start the App:
Run the application using:npm start
-
Props
: Props is short for "properties".- Props are used to pass data (properties or values) from one component to another.
- In simple terms, props help in passing data between components.
-
Props are attributes passed from a parent component to a child component and are accessed using props.propertyName.
- Props are passed as attributes in the JSX tag in the parent component.
- They are received as parameters in the target child component.
<Card title="Card 1" Description="Card 2 Description" />
- React Hooks are special functions that let you use React features, like state and lifecycle methods, in functional components.
-
๐ React Hooks allow you to add functionality to your functional components without using class components.
-
๐ They make it easier to use state, effects, and other React features.
-
๐ฆ Think of hooks as tools that enhance your components.
-
๐ง
useState()
โ Manage State in Functional Components -
The
useState()
The React useState Hook allows us to track state in a function component. -
State generally refers to data or properties that need to be tracking in an application. ๐
-
State is like a variable that React keeps track of.
-
It helps your components remember things, like user input or a counter value.
-
When you use
useState()
, it gives you two things:-
๐งฎ The current value of the state.
-
๐ A function to update the value.
-
import React, { useState } from "react";
function Counter() {
// Initialize state with 0
const [count, setCount] = useState(0);
return (
<div>
<p>Count: {count}</p>
<button onClick={() => setCount(count + 1)} > Increase </button>
</div>
);
}
-
count is the current value of the state.
-
setCount is a function to update the state.
-
The useState Hook can be used to keep track of strings, numbers, booleans, arrays, objects, and any combination of these!
-
We could create multiple state Hooks to track individual values.
import React, { useState } from "react";
function Car() {
const [brand, setBrand] = useState("Ford");
const [model, setModel] = useState("Mustang");
const [year, setYear] = useState("1964");
const [color, setColor] = useState("red");
}
-
๐ Open the
index.html
file located in the public folder. -
๐๏ธ Find the
<title>
tag inside the<head>
section. -
โ๏ธ Change the title to whatever you want, like this:
<title>Zahid Khattak React App</title>