react bootcamp
react bootcamp
Real-life Example: Creating a simple app setup to work like a workspace or kitchen
for building your app.
Module 3: React Components
What are Components?
React components are reusable pieces of UI.
Code Example:
function Welcome() {
return <h1>Hello, World!</h1>;
}
Real-life Example: Components are like building blocks in LEGO, small reusable
parts that come together to form a structure.
Module 4: JSX – The Syntax of React
Introduction to JSX
JSX is a mix of HTML and JavaScript:
const name = "John";
const element = <h1>Hello, {name}!</h1>;
Real-life Example: Like writing a recipe card where you mix ingredients (HTML) and
instructions (JavaScript) in one place.
Day 2: State, Props, and Event Handling
Module 1: Understanding Props
What are Props?
Props are arguments passed into components.
Code Example:
function Greeting(props) {
return <h1>Hello, {props.name}!</h1>;
}
function App() {
return <Greeting name="Alice" />;
}
Real-life Example: Props are like ingredients being passed to different stations in
a kitchen.
Module 2: Introduction to State
What is State?
State allows React components to change and re-render based on data.
Code Example using useState:
import React, { useState } from "react";
function Counter() {
const [count, setCount] = useState(0);
return (
<div>
<p>You clicked {count} times</p>
<button onClick={() => setCount(count + 1)}>Click me</button>
</div>
);
}
Real-life Example: State is like a shopping cart that changes as you add items.
Module 3: Handling Events in React
Event Handling
Handling user interactions like button clicks.
Code Example:
function Button() {
const handleClick = () => {
alert("Button was clicked!");
};
function GuestGreeting(props) {
return <h1>Please sign up.</h1>;
}
function Greeting(props) {
const isLoggedIn = props.isLoggedIn;
if (isLoggedIn) {
return <UserGreeting />;
}
return <GuestGreeting />;
}
function App() {
return <Greeting isLoggedIn={false} />;
}
return (
<ul>
{items.map((item, index) => (
<li key={index}>{item}</li>
))}
</ul>
);
}
function Form() {
const [name, setName] = useState("");
return (
<form onSubmit={handleSubmit}>
<label>
Name:
<input type="text" value={name} onChange={(e) =>
setName(e.target.value)} />
</label>
<button type="submit">Submit</button>
</form>
);
}
useEffect(() => {
fetch("<https://api.example.com/data>")
.then((response) => response.json())
.then((data) => setData(data));
}, []);
return (
<div>
<h1>Fetched Data:</h1>
<pre>{JSON.stringify(data, null, 2)}</pre>
</div>
);
}
Real-life Example: Fetching and displaying data when the component loads.
Module 4: Mini Project To-Do List App
In this mini project, students will build a basic to-do list app to reinforce the
concepts learned over the three days. This project includes components, state,
props, event handling, and conditional rendering.
Project Features:
Add New Task: User can input a task and add it to the list.
Mark Task as Completed: Each task has a button to mark it as complete.
Delete Task: User can delete a task from the list.
Code Example for To-Do List App:
import React, { useState } from "react";
function TodoApp() {
const [tasks, setTasks] = useState([]);
const [input, setInput] = useState("");
return (
<div>
<h1>To-Do List</h1>
<input
type="text"
value={input}
onChange={(e) => setInput(e.target.value)}
placeholder="Add new task"
/>
<button onClick={addTask}>Add Task</button>
<ul>
{tasks.map((task, index) => (
<li key={index}>
<span
style={{ textDecoration: task.completed ? "line-through" : "none" }}
>
{task.text}
</span>
<button onClick={() => toggleComplete(index)}>
{task.completed ? "Undo" : "Complete"}
</button>
<button onClick={() => deleteTask(index)}>Delete</button>
</li>
))}
</ul>
</div>
);
}