,

Beginner’s Tutorial: Building a Todo List App with React JS

Posted by


React Todo List App Tutorial – React JS Project Tutorial for Beginners

React JS is a JavaScript library used for building user interfaces and offers a simplified way of developing interactive web applications. In this tutorial, we will go through the process of building a Todo List app using React JS.

Prerequisites

Before we start, make sure you have the following installed on your machine:

  • Node.js
  • npm – Node Package Manager

Step 1: Set up the project

First, we need to create a new folder for our project. Open your terminal/command prompt and navigate to the desired location. Use the following command to create a new React app:


npx create-react-app todo-list-app

This command will create a new directory called “todo-list-app” with all the necessary files and dependencies to get started.

Next, go into the project directory by running the following command:


cd todo-list-app

Step 2: Create the Todo List component

In the “src” directory, create a new file called “TodoList.js”. This file will contain the code for our Todo List component.

Open “TodoList.js” and add the following code:


import React from 'react';

class TodoList extends React.Component {
constructor(props) {
super(props);
this.state = {
todos: [],
newTodo: ''
};
}

render() {
return (
// Add JSX code here
);
}
}

export default TodoList;

This code sets up the initial state of our Todo List component, with an empty array for the todos and a newTodo variable to track the input value.

Step 3: Add JSX code for the Todo List

In the render() method of the TodoList component, we can now add the JSX code for our Todo List.


render() {
return (

Todo List


    {this.state.todos.map((todo, index) => (

  • {todo}
  • ))}

);
}

This JSX code will render a heading, an input field for adding new todos, a button to add the todo, and an unordered list to display the todos.

Step 4: Implement event handlers

In order for our todo list to work, we need to implement the event handlers for input change and the add button click.

Add the following code inside the TodoList component:


handleInputChange = (event) => {
this.setState({ newTodo: event.target.value });
}

addTodo = () => {
if (this.state.newTodo !== '') {
const updatedTodos = [...this.state.todos, this.state.newTodo];
this.setState({ todos: updatedTodos, newTodo: '' });
}
}

The handleInputChange function updates the state with the value of the input field, while the addTodo function adds the new todo to the todos array and clears the input field.

Step 5: Render the TodoList component

In the “src” directory, open the “index.js” file. Replace the existing code with the following:


import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import TodoList from './TodoList';

ReactDOM.render(


,
document.getElementById('root')
);

This code renders the TodoList component inside the root element of your HTML document.

Step 6: Start the development server

Finally, in the terminal/command prompt, run the following command:


npm start

This command will start the development server and automatically open your Todo List app in your default web browser.

Now you have a functional Todo List app built using React JS! You can add new todos and see them displayed on the screen.

Feel free to explore more React JS concepts and enhance the functionality of your app by adding features like marking todos as completed or deleting them. Happy coding!

0 0 votes
Article Rating
21 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Ope Afolabi
7 months ago

Let me know what video topics you guys want next 🙏

Mrunali Choudhary
7 months ago

Wonderful doubt , But there is problem in this code when i refresh the to do list in my browser the changes that I made(for example added the list of tasks) does not remain same, it vanishes with the reload of page. Can you please help me with this?

FeastOrFeed
7 months ago

The most effective beginner's tutorial I've seen so far.

taufiq islam
7 months ago

why did you use key = {index} as a prop for todo component, you didn't use it in the component

c d
c d
7 months ago

I thank you very much that you care about local storage in source code , thank you very much

Devi Sri
7 months ago

I am not able to connect with local host help me

Magdy Kishk
7 months ago

Somehow you managed to confuse me in the first 10 seconds of the video I still don't understand how can a person eat milk?

Manjuriya
7 months ago

Tq sir usefull vedio

MACSARYSTE
7 months ago

Aint no way this is beginner bro.The little courage i had was dismantled

KIHAL Bilel
7 months ago

Thank you so much bro

Hadia Mazhar
7 months ago

can you explain what have you done at 4:56/31:12?

Ayush
7 months ago

The best part about the video is that you didn't waste time on styling ✌
BTW Awesome Explanation!!!

all"editing
7 months ago

How you import file in app.js
My file is not import

Egor Nikolaev
7 months ago

Bro, awesome tutorial! Tell me please what color theme do you use for vscode?

3016-BHANU Prakash
7 months ago

This is the very good one but could you please also add the features like searching, last modified date in it…

Jivitesh Kanna
7 months ago

I am getting an error

Darioush
7 months ago

Thanks for your nice tutorial but The most important thing in this app is using localStorage to keep data after reloading page which you didn't mentioned that 😐

ANEngineer
7 months ago

hi, I have a question. I'm wondering that how to use 'todo-input' or 'todo-btn' in className? I couldn't any doc for fontawesome. How did you do this appearance? I mean input and button are side by side

nakul rawal
7 months ago

it could have been more better with firebase

Yanni
7 months ago

hey how would i make the Add Task Button go under the text bar but still above the line of the shadowed box?