This tutorial introduces React step by step with simple explanations and examples.
React is a JavaScript library developed by Facebook for building user interfaces, especially single-page applications.
The easiest way to create a React app is using create-react-app:
npx create-react-app my-app cd my-app npm start
This starts a development server at http://localhost:3000.
JSX allows you to write HTML inside JavaScript.
const element = <h1>Hello, React!</h1>;
Components are reusable pieces of UI.
function Welcome() {
return <h1>Welcome to React</h1>;
}
<Welcome />
Props are used to pass data to components.
function Greeting(props) {
return <h2>Hello, {props.name}!</h2>;
}
<Greeting name="Alice" />
State allows components to manage dynamic data.
import { useState } from "react";
function Counter() {
const [count, setCount] = useState(0);
return (
<div>
<p>Count: {count}</p>
<button onClick={() => setCount(count + 1)}>
Increase
</button>
</div>
);
}
Events in React are written in camelCase.
function ClickExample() {
function handleClick() {
alert("Button clicked!");
}
return <button onClick={handleClick}>Click Me</button>;
}
const [name, setName] = useState("");
Runs side effects such as fetching data.
import { useEffect } from "react";
useEffect(() => {
console.log("Component mounted");
}, []);
{isLoggedIn ? <Dashboard /> : <Login />}
const items = ["Apple", "Banana", "Orange"];
React helps you build fast, scalable, and maintainable user interfaces. Practice building small projects to master it.
Next steps: