,

API Calling & Data Fetching in React Js using MERN Stack for Tours Booking Website with Context API and Custom Hooks

Posted by








MERN Stack Tours Booking Website

MERN Stack Tours Booking Website

In this article, we will discuss how to call and fetch data from an API in a React Js application using the MERN stack. We will specifically focus on using the Context API and custom hooks to manage state and make API calls.

Setting up the project

First, we need to set up our MERN stack project. MERN stands for MongoDB, Express, React, and Node.js. We will start by creating a Node.js server and setting up a MongoDB database. Next, we will create a React Js frontend for our tours booking website.

API calling in React Js

Once our project is set up, we can start making API calls from our React Js application. We can use the built-in fetch API or a library like axios to make HTTP requests to our server. This will allow us to retrieve data from our database and display it on our website.

Using Context API

The Context API in React Js allows us to manage global state in our application. We can create a context for our tours data and provide a state and functions to manipulate that state to our components. This will allow us to access the tours data from any component in our application.

Creating Custom Hooks

To make our code more reusable and efficient, we can create custom hooks to handle the logic for fetching data from our API. We can create a custom hook that makes the API call and manages the state for our tours data. This will allow us to easily use the same logic in multiple components throughout our application.

Conclusion

By using the MERN stack, Context API, and custom hooks, we can efficiently manage state and make API calls in our tours booking website. This will allow us to provide a seamless and responsive user experience for our users as they browse and book tours on our website.


0 0 votes
Article Rating
32 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Mahmut Yilmaz
1 year ago

I have received an error in Booking .js. 'the server responded with a status of 500 (Internal Server Error)' Can you help me?

Harshada Dharne
1 year ago

Hi nice job friend amazing websites
But please can you please explain algorithm and some details of your project ❤❤

Pramit De
1 year ago

From where you have taken the tour images because when I am using my images it is not getting rendered but yours images are getting rendered

B VISHWANATH REDDY
1 year ago

In Register.jsx, in the 30th line i'm getting error,but yours is correct and due to this issue i'm unable to register . And also i'm(user) unable to give ratings.Help me to sort out these problems.Nice video and very attractive website.

Arnav Sharma
1 year ago

"success": false,
"message": "You're not authorize while fetching the all tours in tours page its showing unable to fetch

Vilius Z
1 year ago

Good Mern tutorial, thanks.

Rai Usama
1 year ago

Please provide complete source code of this project

Hoàng Anh Nguyễn
1 year ago

"401 (Unauthorized)" When I submit the review 🙁

Praveen kumar chauhan
1 year ago

this is last part ?

Avi Saini
1 year ago

On submitting review after refreshing it shows "failed to fetch" please help me, I am stuck here for very long. Basically review is not saving in database.

Avi Saini
1 year ago

It does not saving the cookies , and give warning like samesite. Please help me I am stuck here for 2 days

Avi Saini
1 year ago

If anyone completed it please share code link

Avi Saini
1 year ago

At 48:59 it shows alert "you are not authorised".

Stranger
1 year ago

why tour image are not displaying for me?

Nazmul Hasan
1 year ago

ERROR
JSON.parse: unexpected character at line 1 column 1 of the JSON data
.
how solve it?

Vicky vinay
1 year ago

Bro where do the crud ops done, in website

Vicky vinay
1 year ago

Bro is there any admin page for this bro..

Ambre Caldin
1 year ago

Thank you for this great tutorial, it's very complete. I was wondering what vs code extensions you use ?

Kumar Gaurav
1 year ago

can you please make a video on deploying the above project on netlify/vercel

Acopsie dashh
1 year ago

Anyone managed to finish this project i need to know