CRUD operation in Firebase V9
Tags: webdev, beginners, react, firebase
Published: 2022-04-06 04:22:14

Today, we will talk about firebase v9 and how to integrate it in a React.js project. So first of all let's see what are we doing today-

1. What is firebase ?
2. Create react.js project with firebase.
3. Config firebase v9.
4. Write Operation.
5. Read Operation.
6. Update Operation.
7. Delete Operation.

**1. What is firebase** - 
Firebase is a platform by Google which provide cloud, database and many other services for mobile and web applications. It provides Realtime Database and API that synchronizes application data. For starting you can use it for free but after some time if you want to use it for high scale then you can pay on demand. you can also use third party hosting and domain in firebase.

**2. Create react.js project with firebase** - 
We are creating a _todo_ react project and we will use firebase for our backend data, so we can create, read, update and delete our todos in realtime.
So, first of all let's create a react project.
```
npx create-react-app todos
```
When you run this command in cmd, when it complete building then you will see a folder called todos. open this folder in any code editor you want.
![react project](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4bpq0cm0g10kjnk23e12.png)

Now we want to install firebase using below command and when it completed , you can find firebase version in package.json file.
```
npm i firebase
```
![firebase version](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qoa3256ua6o62rys6kzm.png)

_Now let's go to firebase website and quickly create a account, click on console, click on web and give the details like project name._

![firebase console](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ek6gnmer51gaeja42b50.png)
 
_Now when you click on web icon <> , you will get screen like below, just copy this config details we want these details in our react app._
![](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uteeclo0moyv9lprldht.png)

**3. Config firebase v9** -
Now our next task is integrate our firebase code in our react app. for that create a files named firebaseInit.js (optional name) under src folder and copy above code there.
![config file](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/grg9fiagqqo3v0rtkf8v.png)
_and we are done, now we can use firebase in our react app as we need. _

**4. Write Operation** -
Let's dive into CRUD operation now. To use database in react first we need to import firestore in our app.

![import firestore](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mcdquipdpga0hl4ho35w.png)

_now we can use db in any component and perform CRUD operation. let's try to Write data in firestore._

![write method](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/75zb7z2bw4jyqd590z7z.png)

_Now if you click on button , then it will addDoc in a collection name "todo" (if it doesn't exist then firebase will make it) with fields describe in line 14,15_

![addDoc](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a4cjbiehbvvcdh0n0efj.png)

**5. Read Operation** -
Now we can send data to firebase , but what about to read it. Meaning there are no sense of a todo if we can't see all todos on screen. then let's create a read component too.
![read component](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7zr2ko0gg83g74tuouyf.png)
_Here, we use useEffect so whenever this component runs it fetches all the data from firestore database. and this is our results_
![data](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oqlzvbkllr9sj5z6aevq.png)
 
**6. Update Operation** -
Now i realized that there are no unicorns in park and i want to change it to theme park, so what are the options? i can use update operation so that i can do it. For simplicity ,we will create update method with read component.

_First we will give a update Button which will open a modal with a form_
![update modal](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hjfzj0yg693isb7zpy98.png)

_Now,as you can see we have a handleUpdate function to update form data._
![handleUpdate](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t2fqvhpuaiaw0fcvknxj.png)

_That's it now let's see our results_
![updated data](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l8zzz1i8pansl87ijsk8.png)

**7. Delete Operation** -
Now we need a delete functionality, so that we can delete a todo when it's done . 
![](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h5an7mo0f3axfj4vsf7r.png)
_Here, we have a handleDelete with id to delete todo._
![](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x9p7hv84xduuzhiej541.png)

## Conclusion
This is the CRUD operation in firebase, if you stuck anywhere then i have provider project link too. you can use and change code as you want . [github repo](https://github.com/Tejendrasrajawat/todo-firebase-project/)