This cheat sheet focuses on core React concepts and the modern JavaScript (ES6+) syntax essential for effective React development β especially the tricky ideas of Props and State.
Definition: React is a framework that combines JavaScript and HTML to address issues like repetitive code and indentation problems. Itβs often described as βall JavaScript.β
Project Setup (Using Vite):
npm create vite@latestcd [project name]npm install β installs dependencies and creates the node_modules foldernpm run dev β starts the local development serverThe Component Function:
Example:
export default function Greeting() {
return <h1>Hello!</h1>;
}
Importing Components:
When you organize components into separate files (typically in a components/ folder), you import them using relative paths:
// In App.jsx
import Greeting from './components/Greeting';
import Avatar from './components/Avatar';
export default function App() {
return (
<>
<Greeting />
<Avatar name="Ash" />
</>
);
}
./ for current directory or ./components/ for subdirectory.jsx is optional in the import statementexport default the componentDefinition: JSX lets you write HTML-like code directly inside JavaScript.
Key Rules:
{} to embed JavaScript inside JSX.className instead of class for CSS.<div> or <>...</>).Example:
return (
<>
<h1>{title}</h1>
<p>{message}</p>
</>
);
Definition: Props are data passed from a parent to a child component.
Usage Example:
<Avatar name="Ash" />
Receiving Props:
export default function Avatar({ name }) {
return <h2>{name}</h2>;
}
Note: Using { name } is called destructuring - it extracts the name property directly from props, making your code cleaner and easier to read.
Children Prop:
<Card>
<p>This content is passed as children.</p>
</Card>
Best Practice: Start without props β then add props to make components reusable.
useState)Purpose:
State allows a component to remember information and update the screen automatically when that information changes β such as a counter, score, or text input. It replaces manual DOM updates like document.getElementById() or innerText.
Import and Declare:
import { useState } from "react";
const [count, setCount] = useState(0);
count β current valuesetCount() β updates the value0 β initial valueRule:
Never update state directly (count = count + 1 β).
Use the setter function instead (setCount(count + 1) β
).
import { useState } from "react";
export default function Counter() {
const [count, setCount] = useState(0);
function handleClick() {
setCount(count + 1);
}
return (
<button onClick={handleClick}>
You clicked {count} times
</button>
);
}
import { useState } from "react";
export default function Greeting() {
const [name, setName] = useState("Ash");
function handleChange() {
setName("Misty");
}
return (
<>
<h2>Hello, {name}!</h2>
<button onClick={handleChange}>Change Name</button>
</>
);
}
π Here, React remembers the name string and automatically updates the text when setName() is called.
Events in React:
onClick, onChange.Example:
function handleClick() {
setCount(count + 1);
}
<button onClick={handleClick}>Add One</button>
| Concept | Description | Example |
|---|---|---|
| Arrow Functions | Shorter function syntax | const greet = () => "Hi"; |
| const / let | Modern variable declarations | const name = "Ash"; |
| Ternary Operator | Inline if inside JSX |
{isOn ? "Yes" : "No"} |
Array .map() |
Loop through items and render | {cats.map(cat => <li>{cat}</li>)} |
Import CSS File:
import './App.css';
Inline Styles:
<div style=>
Hello
</div>
Note: Use camelCase (backgroundColor not background-color).
Bootstrap Integration:
Add to your index.html:
<link
href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css"
rel="stylesheet"
/>
or use React Bootstrap for ready-made components.
β
Tip:
React doesnβt update the DOM directly β instead, it re-renders the component whenever state or props change. Think of it like an automatic updateScreen() each time your data updates.