📖 Scenario: You are building a simple React app that manages a list of tasks. As the app grows, you want to organize your code better by splitting it into smaller components and using a central state management approach.
🎯 Goal: Build a React app that shows a list of tasks, allows adding new tasks, and organizes the code into components with a shared state using React's useState hook.
📋 What You'll Learn
Create a main component with initial task data
Add a configuration variable for the maximum number of tasks allowed
Use a loop to render the list of tasks dynamically
Add a form to add new tasks respecting the maximum limit
💡 Why This Matters
🌍 Real World
Managing state and components in React is essential for building scalable web apps like to-do lists, shopping carts, or dashboards.
💼 Career
React developers often need to organize large apps by splitting UI into components and managing shared state efficiently.
Progress0 / 4 steps
1
Set up initial task data
Create a functional React component called TaskApp. Inside it, create a state variable called tasks using useState with the initial array ["Buy groceries", "Walk the dog", "Read a book"].
React
Hint
Use const [tasks, setTasks] = useState([...]) inside the TaskApp function.
2
Add a maximum tasks limit
Inside the TaskApp component, create a constant called maxTasks and set it to 5. This will limit how many tasks can be added.
React
Hint
Just add const maxTasks = 5; inside the TaskApp function below the state.
3
Render the list of tasks
Inside the TaskApp component, return a <ul> element that uses tasks.map((task, index) => ...) to create a list of <li> elements. Each <li> should have a key set to index and display the task text.
Inside the TaskApp component, add a form with an input and a button. Use a state variable newTask to track the input value. On form submit, add newTask to tasks only if the number of tasks is less than maxTasks. Clear newTask after adding. Render the form below the task list.
React
Hint
Use useState for newTask. Add a form with onSubmit={handleSubmit}. In handleSubmit, prevent default, check conditions, update tasks, and clear input.
Practice
(1/5)
1. What is a common way to organize a large React application to keep it manageable?
easy
A. Use only class components to keep consistency
B. Write all code in a single file for easy access
C. Avoid using components and write plain JavaScript
D. Split the app into smaller components and folders
Solution
Step 1: Understand component-based design
React encourages breaking UI into small, reusable components.
Step 2: Organize code in folders
Grouping related components and files in folders helps maintain large apps.
Final Answer:
Split the app into smaller components and folders -> Option D
Quick Check:
Component + folder structure = A [OK]
Hint: Think modular: small parts in folders make apps manageable [OK]
Common Mistakes:
Putting all code in one file makes it hard to maintain
Ignoring components loses React's main advantage
Using only class components is outdated
2. Which of the following is the correct way to import a component named Header from a file in React?
easy
A. import Header from './Header';
B. require Header from './Header';
C. import { Header } from './Header.js';
D. include Header from './Header';
Solution
Step 1: Recognize ES6 import syntax
React uses ES6 modules, so import Header from './Header'; is correct for default exports.
Step 2: Check other options
require is CommonJS, not standard in React apps; include is invalid; named import { Header } requires named export.
Final Answer:
import Header from './Header'; -> Option A
Quick Check:
Default import uses import X from [OK]
Hint: Use ES6 import syntax: import Component from 'path' [OK]