Note: This assumes you're working inside a Vite + React project and are following best practices such as:
JSX lets you write HTML in JavaScript. It's syntactic sugar for React.createElement()
.
const Welcome = () => {
return (
<div>
<h1>Hello, world!</h1>
</div>
);
};
className
instead of class
.Manages local state inside a functional component.
import { useState } from "react";
const Counter = () => {
const [count, setCount] = useState(0);
const increment = () => setCount(count + 1);
return (
<div>
<p>Count: {count}</p>
<button onClick={increment}>Increment</button>
</div>
);
};
Performs side effects in a component (e.g., fetching data, timers).
import { useEffect, useState } from "react";
const fetchData = async (setData) => {
const res = await fetch("https://api.example.com/data");
const json = await res.json();
setData(json);
};
const DataFetcher = () => {
const [data, setData] = useState(null);
useEffect(() => {
fetchData(setData);
}, []);
return <div>{data ? <p>{data.message}</p> : <p>Loading...</p>}</div>;
};
Used to pass data from parent to child components.
const Greeting = ({ name }) => {
return <h1>Hello, {name}!</h1>;
};
const App = () => {
return <Greeting name="John" />;
};
Render elements based on a condition.
const Auth = ({ isLoggedIn }) => {
return <div>{isLoggedIn ? <p>Welcome back!</p> : <p>Please log in.</p>}</div>;
};
Control input values with state.
import { useState } from "react";
const ContactForm = () => {
const [email, setEmail] = useState("");
const handleSubmit = (e) => {
e.preventDefault();
alert(`Submitted: ${email}`);
};
return (
<form onSubmit={handleSubmit} className="flex flex-col gap-2">
<input
className="border p-2 rounded"
type="email"
value={email}
onChange={(e) => setEmail(e.target.value)}
/>
<button
type="submit"
className="bg-blue-500 text-white px-4 py-2 rounded hover:bg-blue-700">
Send
</button>
</form>
);
};
const ItemList = () => {
const items = ["Apple", "Banana", "Cherry"];
return (
<ul className="list-disc pl-5">
{items.map((item, index) => (
<li key={index}>{item}</li>
))}
</ul>
);
};
Access DOM elements directly.
import { useRef } from "react";
const FocusInput = () => {
const inputRef = useRef();
const handleClick = () => {
inputRef.current.focus();
};
return (
<div className="flex flex-col gap-2">
<input ref={inputRef} type="text" className="border p-2 rounded" />
<button
onClick={handleClick}
className="bg-blue-500 text-white px-4 py-2 rounded hover:bg-blue-700">
Focus Input
</button>
</div>
);
};
Pass global data without prop drilling.
import { createContext, useContext } from "react";
const ThemeContext = createContext("light");
const ThemedComponent = () => {
const theme = useContext(ThemeContext);
return (
<div
className={`p-4 ${
theme === "dark" ? "bg-gray-800 text-white" : "bg-white text-black"
}`}>
The current theme is {theme}
</div>
);
};
const App = () => {
return (
<ThemeContext.Provider value="dark">
<ThemedComponent />
</ThemeContext.Provider>
);
};
import { BrowserRouter as Router, Routes, Route, Link } from "react-router-dom";
const Home = () => {
return <h2>Home</h2>;
};
const About = () => {
return <h2>About</h2>;
};
const App = () => {
return (
<Router>
{/**********************************************
For best practice anything for navbar or footer
should be in its own component, below we are
just demonstrating
***********************************************/}
<nav className="flex gap-4 p-4 bg-gray-100">
<Link to="/" className="text-blue-500 hover:underline">
Home
</Link>
<Link to="/about" className="text-blue-500 hover:underline">
About
</Link>
</nav>
<Routes>
<Route path="/" element={<Home />} />
<Route path="/about" element={<About />} />
</Routes>
</Router>
);
};
Utility | Explanation |
---|---|
flex , grid |
Use for layout systems |
flex-col |
Stack children vertically |
justify-center |
Horizontally center items |
items-center |
Vertically center items |
gap-4 |
Add space between flex/grid items |
Utility | Explanation |
---|---|
m-4 , p-4 |
Margin/Padding on all sides (1rem) |
mt-2 , mb-4 |
Margin top/bottom only |
px-6 , py-2 |
Padding x/y direction |
Utility | Explanation |
---|---|
w-full |
Full width of parent |
w-1/2 |
50% width |
h-screen |
Full height of the viewport |
Utility | Explanation |
---|---|
bg-blue-500 |
Background color |
text-white |
White text |
hover:bg-blue-700 |
Changes on hover |
Utility | Explanation |
---|---|
text-sm , text-2xl |
Font size |
font-bold , font-light |
Font weight |
text-center , text-left |
Text alignment |
Utility | Explanation |
---|---|
border |
Adds default border |
border-2 , border-t |
Thickness and side control |
rounded , rounded-xl |
Border-radius values |
Utility | Explanation |
---|---|
relative , absolute |
Control positioning context |
top-0 , left-0 |
Position offsets |
z-10 , z-50 |
Stack order |
Prefix | Screen Size |
---|---|
sm: |
≥ 640px |
md: |
≥ 768px |
lg: |
≥ 1024px |
xl: |
≥ 1280px |
2xl: |
≥ 1536px |
Example:
<div className="text-sm md:text-lg lg:text-xl">Responsive Text</div>
This makes the text: