React Tutorial: How to develop an application from scratch with modern libraries
Carlos Azaustre
Posted on September 26, 2023
Introduction
In this step-by-step tutorial, we are going to learn how to develop a React web application from scratch using some of the most popular tools in the React ecosystem.
Specifically, we will use:
Zustand for global state management
React Query for data fetching and caching
React Hook Form for form handling
React Router for navigation
React Hot Toast for showing notifications
The application we will build will simulate a hotel booking system, with a page to list hotels and a details page to book rooms in a specific hotel.
Here are some of the features we will implement:
Fetch and display a list of hotels from an API
Navigate to the detail page of a specific hotel
Form to select dates and book a room
Global state management for bookings
Notifications on errors or successful bookings
Form validations
So without further ado, let's get started!
Preparing the project
The first thing we need to do is prepare our React project. For this, we will use create-react-app which will allow us to bootstrap quickly:
npx create-react-app hotel-reservations
This will generate the basic React project structure in a folder called hotel-reservations.
Next, we install the dependencies by running npm install inside that folder.
Our project is now setup and ready! We can now start coding.
Global state with Zustand
First we are going to configure our global state using Zustand.
Zustand is a lightweight library for managing global state in React. It allows us to have a centralized store that can be accessed from any component to read and update state.
For this, we create a store.js file and add the following:
Basically we are creating a useStore hook that gives us access to the reservations global state and an addReservation action to add new bookings.
We can import and use this hook from any component in our app.
Data fetching with React Query
Now we are going to take care of fetching data from an API. For this we will use React Query.
React Query allows us to perform data fetching, caching, and updating. It also handles loading, error and automatic revalidation states.
First, we create a db.json file with mock data:
{"hotels":[{"id":1,"name":"Hotel Sunshine","description":"Beachfront hotel with pool","image":"hotel-1.jpg"},{"id":2,"name":"Hotel Mountain Spa","description":"Luxury hotel in the mountains","image":"hotel-2.jpg"}]}
Then, we create a script in package.json to spin up a dev server with that data:
// HotelsList.jsimport{fetchHotels}from'./hotels'import{useQuery}from'react-query'exportdefaultfunctionHotelsList(){const{data,error,isLoading}=useQuery('hotels',fetchHotels)// display data or loading/error statereturn (// JSX to display hotels)}
That's all we need to implement data fetching with React Query!
Forms with React Hook Form
Now let's implement the booking form with React Hook Form.
React Hook Form allows us to build forms easily in React, with validations, error handling and state management.
First we create our BookingForm component:
// BookingForm.jsimport{useForm}from'react-hook-form'functionBookingForm(){const{register,handleSubmit,formState:{errors}}=useForm()constonSubmit=(data)=>{// submit form}return (<form onSubmit={handleSubmit(onSubmit)}><input{...register('checkinDate', { required: true })}/>{errors.checkinDate && (<p>Checkin date required</p>)}// ...more inputs and validations</form>)}
We are registering the inputs so React Hook Form controls them, handling submit, and displaying any validation errors.
And that's all we need for powerful form handling with React Hook Form!
Navigation with React Router
Now we are going to configure navigation between different views using React Router.
First, we wrap our app with a <BrowserRouter> in index.js.
Hotel booking application, using React along with some of the most popular and powerful libraries in the ecosystem: React/TanStack Query, Zustand, Wouter, React Hooks Forms, React Hot Toast, and Material UI
Integration and use of React/TanStack Query to manage queries and mutations.
Global state with Zustand.
Navigation with Wouter.
Forms with React Hooks Forms.
Notifications with React Hot Toast.
Design and styles with Material UI.
This tutorial is perfect for both beginners looking for a detailed "react js tutorial" and experienced developers wanting to expand their knowledge and learn about new libraries.
You don't need to download anything beforehand; I'll show you how to do everything from the beginning.
So, prepare your development environment and join me on this exciting journey through the world of React JS!
En este tutorial de React JS desde cero te guiaré paso a paso en la creación de una aplicación de reservas de Hotel, utilizando React junto con algunas de la...
youtube.com
I hope you found this tutorial useful! Don't forget to share it and follow me for more web development content. See you next time!