Maximizing your React development with ChatGPT: A step-by-step guide to building a task list app
- tejender7
- Feb 16, 2023
- 5 min read

React is a popular JavaScript library for building user interfaces, and developers often use it to create a variety of web applications. However, sometimes it can be challenging to get started and iterate over requirements to build an application. That's where ChatGPT can help. ChatGPT is a large language model trained by OpenAI that can answer questions and provide assistance on a wide range of topics, including React development.
In this article, we will walk through how ChatGPT can assist a React developer in creating a "to-do" list application with various features. We will cover each step in detail.
Step 1: Create a React Application
To create a React application, the first step is to set up a basic project with the necessary dependencies and tooling. ChatGPT can help with this by providing guidance on tools and libraries for creating a React application. Developers can give commands such as:

This creates a basic React application which we can build upon.

Step 2: Create a Component to Display the List of Tasks
Once the project is set up, the next step is to create a component that displays the list of tasks. ChatGPT can provide guidance on how to create a React component and display data in it. Developers can ask questions such as "How can I create a React component to display a list of tasks?" or "What is the best way to display data in a React component?" ChatGPT can provide sample code snippets and explanations on how to achieve this.

This gives us the following component

Step 3: Add a Text Box in App Component to Receive Inputs from the User
After creating the component to display the list of tasks, the next step is to add a text box to receive inputs from the user. ChatGPT can provide guidance on how to create a text box in a React component and how to handle user input. Developers can ask questions such as "How can I add a text box in a React component to receive inputs from the user?" or "What is the best way to handle user input in a React component?" ChatGPT can provide sample code snippets and explanations on how to achieve this.

Here's how the updated application looks like:

Step 4: Store and Fetch Tasks from Browser Cookies
To store and fetch tasks from browser cookies, developers can ask ChatGPT questions such as "How can I store and fetch data from browser cookies in a React application?" or "What is the best way to handle data persistence in a React application?" ChatGPT can provide sample code snippets and explanations on how to achieve this.

This generates the following application:
There's a bug in the above application, when the user refreshes the screen, it should fetch the old tasks from browser cookies but it fails because we use two useEffect in our App Component. Let's modify the code to use one useEffect to update task list in browser storage and initialize the tasks using useState. The updated code looks like this:
function App() {
const [tasks, setTasks] = useState(
JSON.parse(localStorage.getItem("tasks") || "[]")
);
const [inputValue, setInputValue] = useState("");
useEffect(() => {
localStorage.setItem("tasks", JSON.stringify(tasks));
}, [tasks]);
}
This fixes our bug:
Step 5: Add a Delete Button for Each Task in Task List Component
After tasks are displayed in the component, the next step is to add a delete button for each task to allow the user to delete a task. Developers can ask ChatGPT questions such as "How can I add a delete button for each task in a React component?" or "What is the best way to handle user actions in a React component?" ChatGPT can provide sample code snippets and explanations on how to achieve this.


This is how the application looks like after we add a delete button for each task:
Step 6: Add a Clear Button to Clear the List of Tasks
The next step is to add a clear button to clear the list of tasks. Developers can ask ChatGPT questions such as "How can I add a clear button in a React component?" or "What is the best way to handle user actions in a React component?" ChatGPT can provide sample code snippets and explanations on how to achieve this.

Here's how the application looks after we add clear tasks button:
Step 7: Modify Tasks to Be of Two Types
In some applications, tasks can be of different types. For example, in a task manager application, tasks can be categorized as personal or work-related. In this step, we modified the tasks in our application to be of two types: open and closed.
To achieve this, we added a type field to each task object in the App component's state. The type field is either "open" or "closed". We also updated the TaskList component to display two separate lists: one for open tasks and another for closed tasks.


This is how the application looks with tasks separated into open and closed:
Step 8: Add Buttons to Convert Task Types
To allow the user to switch a task's type, we added buttons to each task in the TaskList component. These buttons allow the user to move a task between the open and closed lists.
We used the onClick event to listen for button clicks and update the task's type field. When the type field changes, the TaskList component re-renders and the task moves to the appropriate list.

Step 9: Add design to the Application
Adding design to the application can make it look more appealing and user-friendly. In this step, we will add some design to the task list component. We will add a border around each task, and align the buttons to the right.

Here's how the application looks with new design:

Step 10: Implement Drag and Drop Functionality
The final step in our application is to implement drag and drop functionality. This will allow users to move tasks between the open and closed lists.
React provides support for using HTML drag and drop events to create drag and drop functionality. In this step, we will use HTML drag and drop to enable drag and drop reordering of tasks within the list and moving tasks between the "open" and "closed" sections. First, we will add the onDragStart, onDragOver, and onDrop event handlers to the Task component:

Since we have two different types of lists we will have to detect which list we are dropping the item in. We ask ChatGPT to give us an example which we can implement in our application.

After making few minor modifications this is how our application looks like:
You can view the complete code for the application here.
You can view the live application here.
Conclusion
In this article, we have demonstrated how ChatGPT can assist a React developer in creating a task manager application. We started by creating a simple task list component, then added the ability to store tasks in browser cookies.
We then expanded the application to support multiple task types, and added buttons to allow the user to move tasks between types. Finally, we implemented drag and drop functionality to allow users to move tasks between types.
Using ChatGPT, we were able to create a fully functional task manager application with advanced features in just a few steps. With its vast knowledge base and powerful natural language processing capabilities, ChatGPT can be a valuable tool for any developer looking to streamline their workflow and improve their productivity.
However, it's important to note that ChatGPT is not a substitute for a skilled and experienced developer. While ChatGPT can offer suggestions and automate certain tasks, it does not have the same level of understanding and decision-making abilities as a human developer.
Furthermore, ChatGPT has limitations in terms of its knowledge and experience. ChatGPT's knowledge is limited by the data it was trained on, which currently only goes up to 2021. It may not be familiar with more recent React updates or cutting-edge techniques.
In summary, ChatGPT can be a valuable resource for React developers, especially when it comes to more routine or repetitive tasks. However, it should be used in conjunction with human expertise and judgment, and its limitations should be taken into consideration.
Check out my portfolio at https://tejender-singh.github.io/
Comments