Are you ready to create your first React application? This guide will walk you through building a simple task management app while introducing fundamental React concepts. By the end, you'll have a working application and a solid foundation in React development.
Prerequisites
Before we begin, ensure you have:
- Node.js installed on your computer
- A code editor (VS Code recommended)
- Basic knowledge of HTML, CSS, and JavaScript
Step 1: Setting Up Your Development Environment
First, let's create a new React application using Create React App. Open your terminal and run:
npx create-react-app task-manager
cd task-manager
npm start
This creates a new React project and starts the development server. Your browser should automatically open to http://localhost:3000.
Step 2: Clean Up the Initial Project
Navigate to src/App.js
and replace its contents with this basic structure:
import React from 'react';
import './App.css';
function App() {
return (
<div className="App">
<h1>My Task Manager</h1>
</div>
);
}
export default App;
Clear the contents of src/App.css
and add some basic styling:
.App {
max-width: 600px;
margin: 0 auto;
padding: 20px;
}
.task-form {
margin-bottom: 20px;
}
.task-list {
list-style: none;
padding: 0;
}
.task-item {
display: flex;
justify-content: space-between;
align-items: center;
padding: 10px;
margin: 5px 0;
background-color: #f8f9fa;
border-radius: 4px;
}
.task-item button {
background-color: #dc3545;
color: white;
border: none;
padding: 5px 10px;
border-radius: 4px;
cursor: pointer;
}
Step 3: Creating Your First Component
Let's create a TaskForm component. Create a new file src/components/TaskForm.js
:
import React, { useState } from 'react';
function TaskForm({ onAddTask }) {
const [taskText, setTaskText] = useState('');
const handleSubmit = (e) => {
e.preventDefault();
if (taskText.trim()) {
onAddTask(taskText);
setTaskText('');
}
};
return (
<form onSubmit={handleSubmit} className="task-form">
<input
type="text"
value={taskText}
onChange={(e) => setTaskText(e.target.value)}
placeholder="Enter a new task"
/>
<button type="submit">Add Task</button>
</form>
);
}
export default TaskForm;
Step 4: Creating the Task List Component
Create another file src/components/TaskList.js
:
import React from 'react';
function TaskList({ tasks, onDeleteTask }) {
return (
<ul className="task-list">
{tasks.map((task, index) => (
<li key={index} className="task-item">
<span>{task}</span>
<button onClick={() => onDeleteTask(index)}>Delete</button>
</li>
))}
</ul>
);
}
export default TaskList;
Step 5: Putting It All Together
Update your App.js
to use these components:
import React, { useState } from 'react';
import TaskForm from './components/TaskForm';
import TaskList from './components/TaskList';
import './App.css';
function App() {
const [tasks, setTasks] = useState([]);
const addTask = (text) => {
setTasks([...tasks, text]);
};
const deleteTask = (index) => {
const newTasks = tasks.filter((_, i) => i !== index);
setTasks(newTasks);
};
return (
<div className="App">
<h1>My Task Manager</h1>
<TaskForm onAddTask={addTask} />
<TaskList tasks={tasks} onDeleteTask={deleteTask} />
</div>
);
}
export default App;
Understanding the Code
Let's break down what we've built:
Component Structure
- App.js: The main component that manages the state and coordinates between components
- TaskForm: Handles task input and submission
- TaskList: Displays tasks and manages deletion
Key React Concepts Used
- State Management: Using the
useState
hook to manage tasks - Props: Passing data and functions between components
- Event Handling: Managing form submissions and button clicks
- Components: Breaking the UI into reusable pieces
- Lists and Keys: Rendering arrays of data with unique identifiers
Adding Features
Now that you have a basic application working, try adding these features:
- Task completion toggle
- Task editing
- Local storage persistence
- Task categories or priorities
- Due dates for tasks
Common Issues and Solutions
- Component not rendering: Check if you've exported and imported it correctly
- State not updating: Ensure you're using the state setter function correctly
- Event handlers not working: Verify your function bindings and event syntax
- CSS not applying: Check your class names and CSS selectors
Best Practices
- Keep components small and focused
- Use meaningful names for components and functions
- Handle edge cases (empty input, duplicate tasks)
- Add proper comments and documentation
- Follow React's naming conventions
Next Steps
To continue learning React:
- Add more features to this application
- Learn about React Router for navigation
- Explore state management with Context API or Redux
- Study React Hooks in depth
- Learn about API integration
Conclusion
Congratulations! You've built your first React application. While this is a simple example, it demonstrates many fundamental React concepts. As you continue learning, you'll discover how to build more complex and feature-rich applications.
Remember, the best way to learn is by doing. Try modifying this application, adding new features, or building something completely different using what you've learned.
About Lavesh Katariya
Innovative Full-Stack Developer | Technical Team Lead | Cloud Solutions Architect
With over a decade of experience in building and leading cutting-edge web application projects, I specialize in developing scalable, high-performance platforms that drive business growth. My expertise spans both front-end and back-end development, making me a versatile and hands-on leader capable of delivering end-to-end solutions.