Data Fetching & HTTP Requests
Sending & Receiving Data via HTTP
How To Connect a Backend / Database
Fetching Data
Sending Data
0. Starting Project & Dummy Backend API
run npm install
run npm run dev
create a README.md
file
1. Preparing the App For Data Fetching
connect this React app with this dummy backend
write cd backend
in the terminal
run npm install
& run node app.js
to run the backend
add a new availablePlaces
state with an empty array as initial state in AvailablePlaces.jsx
2. How NOT To Send HTTP Requests (And Why It's Wrong)
use the fetch
function to fetch the data from the backend API
you are not allowed to use the await
& async
keywords in a React component
call setAvailablePlaces
inside of the fetch
function to get back the data
3. Sending HTTP Requests (GET Request) via useEffect
use useEffect
to fix this problem of infinite loop
make the image files available with help of app.use(express.static('images'));
in the backend
send a request to for those image files to the backend in the frontend in Places.jsx
use async
/ await
inside useEffect()
by creating a fetchPlaces()
function in AvailablePlaces.jsx
remove the old code
5. Handling Loading States
show some loading text whilst we are waiting for the data to arrive in case of bad network in AvailablePlaces.jsx
inside of Places.jsx
, accept the isLoading
& loadingText
props
use these props to show some special output
control the isLoading
prop dynamically depending on the progress of the HTTP request
add a new isFetching
state to manage the loading state when fetching the data in AvailablePlaces.jsx
handle the error response in AvailablePlaces.jsx
with help of the response.ok
property
throw
an Error
& wrap the code with try
/ catch
to prevent the application from crashing
add an error
state & use it to update the UI in case of error by rendering the Error.jsx
component
replace the Error.jsx
component by ErrorMessage.jsx
to avoid conflict with the Error
class name
7. Transforming Fetched Data
fetch the user's location before setting the available places with help of the navigator
object in AvailablePlaces.jsx
sort the available places by calling sortPlacesByDistance
from loc.js
set the sortedPlaces
as the available places setAvailablePlaces
inside of the navigator
function
since you can't to use await
on navigator
, setIsFetching
has to be moved elsewhere because it will be called too early
move setIsFetching(false)
into the navigator
callback function after setting the setAvailablePlaces(sortedPlaces)
call it again after setting the error
if we have one
8. Extracting Code & Improving Code Structure
create a helper file named http.js
& place the data fetching code from AvailablePlaces.jsx
inside of it
use this fetchAvailablePlaces
function in AvailablePlaces.jsx
9. Sending Data with POST Requests
send the updated selection of places to the backend in App.jsx
send the request in handleSelectPlace
after updating the userPlaces
state with help of the fetch
function
define a new updateUserPlaces
function in the http.js
helper file & call it in App.jsx
pass the old userPlaces
state & the newly selectedPlaces
in front of it
await
this function & decorate the handleSelectPlace
function with async
use try
/ catch
to wrap this updateUserPlaces
function to catch potential errors
select a place in the application & check the new data inserted in data/user-places.json
10. Using Optimistic Updating
don't show a loading spinner or a loading text as you did in AvailablePlaces.jsx
catch & handle error in App.jsx
inform the user in case of error
manage a new errorUpdatingPlaces
state to update if updating your places goes wrong
show a modal to inform the user
make the modal closable & clear the error message with help of a new handleError
function
display the modal only if there is an error to avoid bug by wrapping the <ErrorMessage>
component with a condition
11. Deleting Data (via DELETE HTTP Requests)
in App.jsx
, turn the handleRemovePlace
function into an async
function
perform optimistic updating by first updating the userPlaces
state
then by sending a HTTP request with help of the updateUserPlace
helper function
add the userPlaces
state as a dependency to the useCallback
dependencies array
wrap this HTTP request with a try
/ catch
& handle the error
12. Practice: Fetching Data
fetch the places stored by the user in App.jsx
with help of useEffect
to avoid an inifinite loop when you update the state
in http.js
, define a new function named fetchUserPlaces
in App.js
, call the fetchUserPlaces
function inside of useEffect
catch & handle error
manage some loading & error states
show conditionally the error in case of error or the places in case there is no error