Building a Simple Tasks App with React 📝
In this tutorial, we'll show you how to create a simple to-do app, also known as a To-Do List, using React, one of the most popular JavaScript libraries for building user interfaces.
Preparing the Environment
Before you start, make sure you have Node.js and npm installed on your computer. If you don't have them yet, you can download them here. Next, you'll need to install Create React App, a tool that lets you easily create new React projects:
npx create-react-app todo-app
Building the Application
Let's start building our to-do app. Create a new component called TodoItem
in a file called TodoItem.js
. This component will represent a single task in our list:
import React from 'react';function TodoItem(props) { return ( <div> <input type="checkbox" /> <p>{props.item.text}</p> </div> );}export default TodoItem;
Now, let's create a TodoList
component that will render a list of TodoItem. For simplicity, let's start with a static list of tasks:
import React from 'react';import TodoItem from './TodoItem';class TodoList extends React.Component { constructor() { super(); this.state = { all: [ { text: 'Wash the dishes' }, { text: 'Clean house' }, { text: 'Shopping' }, ], }; } render() { const todoItems = this.state.todos.map(item => ( <TodoItem key={item.text} item={item} /> )); return <div>{todoItems}</div>; }}export default TodoList;
Running the Application
To start the application, run the following command in the project root directory:
npm start
This will start the development server and open the application in the browser.
Conclusion
Congratulations! You've created a basic to-do app with React. Obviously, this is just a start. You can expand this app to let you add new tasks, mark tasks as complete, and more!
To continue learning and improving your React skills, check out the article on JavaScript and React: Creating Your First Component.