The Wayback Machine - https://web.archive.org/web/20221118234720/https://www.geeksforgeeks.org/how-to-do-crud-operations-in-reactjs/
Skip to content
Related Articles

Related Articles

How to do crud operations in ReactJS ?

Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 18 Aug, 2022
Improve Article
Save Article

CRUD stands for Create, Read, Update, and Delete. In ReactJS everything is aligned in a form of a component and every component has its own way and feature to do so.

React js is one of the most used JavaScript libraries for frontend development. It’s very important to have the basic functionality of CRUD operations in with react-js. In this article, we don’t use any JSON-server or Axios for performing the CRUD operation. We will use a simple JavaScript object to do so. 

For learning CRUD operation with ReactJS and NodeJS please refer to How to build a basic CRUD app with Node.js and ReactJS?

  • Create: Creating a post or adding the table row, adding data into the webpage, or creating content.
  • Read: Reading or retrieving data from web-page
  • Update: Updating or editing existing content in the webpage
  • Deleting: Deleting and removing the entry or content/post from the webpage

In this article, we will be learning to add the name and age of a user and perform CRUD operations into it.

Step 1: Setting up a project

Make sure you have node-js installed on your computer in order to create react app. Enter these commands for creating react app

npx create-react-app crud_app
or
yarn create react-app crud_app

After important installations, enter these commands on terminal/cmd

cd crud_app
npm start or yarn start

This will start the application

Step 2: Project structure

You may get rid of other files like the logo, and other test files and delete them. Here are 4 files in components that are a core of this application in the source folder.

  1. array.js – This file is not a react file but a simple JavaScript object file (Our temporary data source). This object has 3 key-values id, name, and age.
  2. Create.js – This file consist logic for creating the post or adding a table row in out app.
  3. Edit.js – This file is responsible for editing the existing entry.
  4. Home.js – This file is a reading file and responsible for showing an information/data.

Step 3: Installing dependency

First of all download this dependency-

DependencyInstallation commandUsing purpose
React-Bootstrap

npm install react-bootstrap bootstrap@5.1.3

or

yarn add react-bootstrap bootstrap@5.1.3

Bootstrap is a CSS framework that react-router will help us for focusing on functionality rather than on CSS.
React Router Dom

npm install react-router-dom

or

yarn add react-router-dom

We use react router dom for various packages and modules like routers for routing, useNavigations and etc.

Now, after installations, please create make a folder inside an src folder naming components. Then make 4 js files

Create.js
Edit.js
Home.js
array.js

Step 4: Component files – creating files

array.js

 

app.js:This file is used to manage all other component files and provide routing to all other components.

app.js

 

Step 5: Create component

Create.js

 

Output:

 

Home.js

 

Here, I had used localstorage for storing a value and using the values in other pages. You can skip this if you use react-context-api or redux for instance.

Output:

 

Edit.js

 

Output:

My Personal Notes arrow_drop_up
Next
Related Articles
Article Contributed By :
Vote for difficulty
Current difficulty : Easy

Start Your Coding Journey Now!