Ch-19 Building A CRUD App In React

10 May 2024

Ch-19 Building A CRUD App In React

Creating a CRUD (Create, Read, Update, Delete) application in React is an excellent way to understand and practice state management with the useState hook. This guide will walk you through the essential steps to build a simple CRUD app using React and useState.


Overview of the CRUD App

A typical CRUD application allows users to:

  • Create new items
  • Read and display items
  • Update existing items
  • Delete items


We’ll build a basic example of a CRUD app that manages a list of items, such as a list of tasks or notes.


Steps to Build a CRUD App


1. Set Up the Project

Begin by setting up your React project. Create a new React application using Create React App or another setup method of your choice. Ensure your development environment is ready to handle React development.


2. Define the State

Use the useState hook to create the state variables that will manage the list of items. Typically, you will need:

  • An array to hold the list of items
  • State variables to handle input data for new items and edits


3. Create New Items

Implement functionality to add new items to the list. This involves:

  • Rendering a form or input fields for the user to enter details of the new item
  • Managing form input through state variables
  • Updating the items array in state when the form is submitted


4. Read and Display Items

Render the list of items on the screen. Each item should be displayed with its details, such as a name or description. This involves mapping over the state array and rendering each item in the UI.


5. Update Existing Items

Allow users to edit existing items. This requires:

  • Rendering edit buttons or forms next to each item
  • Handling form input to update the state variables
  • Updating the items array in state when the edit form is submitted


6. Delete Items

Provide functionality to remove items from the list. This involves:

  • Rendering delete buttons next to each item
  • Removing the selected item from the state array when the delete button is clicked


7. Manage State Immutably

Ensure that all state updates are done immutably. When adding, updating, or deleting items, create new arrays with the

updated data rather than modifying the existing state directly. This approach ensures predictable state changes and helps React efficiently re-render components.


Detailed Steps


Initialize State

Start by initializing the state to hold the list of items. Typically, you will use an array to store items. You might also initialize state variables for form inputs.


Create New Items

To create new items:

  1. Render an input form where users can enter details for the new item.
  2. Use state variables to manage the form input.
  3. Handle form submission to update the state array with the new item.


Read and Display Items

To display items:

  1. Map over the state array to render each item in the UI.
  2. Ensure each item is displayed with necessary details such as name or description.
  3. Optionally, provide additional information or actions (like edit and delete buttons) next to each item.


Update Existing Items

To update items:

  1. Render edit buttons or forms next to each item.
  2. Use state variables to manage the input for editing.
  3. Handle form submission to update the state array with the modified item.
  4. Ensure the updated array replaces the old array in the state.


Delete Items

To delete items:

  1. Render delete buttons next to each item.
  2. Handle the delete button click to remove the selected item from the state array.
  3. Update the state with the new array that excludes the deleted item.


Immutability in State Updates

When updating state in React, always ensure you do it immutably:

  • For adding items, use the spread operator to create a new array with the existing items and the new item.
  • For updating items, use the map method to create a new array with the updated item.
  • For deleting items, use the filter method to create a new array that excludes the deleted item.


Example Scenario

Consider building a simple task manager as a CRUD app:

  1. Create Task: Users can add new tasks through an input form. Each task will have a name and a status.
  2. Read Tasks: Display the list of tasks. Each task will be shown with its name and status.
  3. Update Task: Users can mark tasks as completed or edit the task name.
  4. Delete Task: Users can remove tasks from the list.


By following these steps, you will effectively manage the task list's state and implement all CRUD functionalities.


Conclusion

Building a CRUD application in React using the useState hook provides a comprehensive way to practice state management. By creating, reading, updating, and deleting items, you gain hands-on experience with handling complex state interactions and ensuring immutability in React. This foundational knowledge is crucial for developing more advanced and dynamic React applications.


Notes and Source Code