- How To Connect a Backend / Database
- Fetching Data
- Sending Data
- run
npm install
- run
npm run dev
- create a
README.md
file
- connect this React app with this dummy backend
- write
cd backend
in the terminal - run
npm install
& runnode app.js
to run the backend - add a new
availablePlaces
state with an empty array as initial state inAvailablePlaces.jsx
- 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 thefetch
function to get back the data
- 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
insideuseEffect()
by creating afetchPlaces()
function inAvailablePlaces.jsx
- remove the old code
- 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 theisLoading
&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 inAvailablePlaces.jsx
- handle the error response in
AvailablePlaces.jsx
- with help of the
response.ok
property throw
anError
& wrap the code withtry
/catch
to prevent the application from crashing- add an
error
state & use it to update the UI in case of error by rendering theError.jsx
component
- with help of the
- replace the
Error.jsx
component byErrorMessage.jsx
to avoid conflict with theError
class name
- fetch the user's location before setting the available places with help of the
navigator
object inAvailablePlaces.jsx
- sort the available places by calling
sortPlacesByDistance
fromloc.js
- set the
sortedPlaces
as the available placessetAvailablePlaces
inside of thenavigator
function - since you can't to use
await
onnavigator
,setIsFetching
has to be moved elsewhere because it will be called too early- move
setIsFetching(false)
into thenavigator
callback function after setting thesetAvailablePlaces(sortedPlaces)
- call it again after setting the
error
if we have one
- move
- create a helper file named
http.js
& place the data fetching code fromAvailablePlaces.jsx
inside of it - use this
fetchAvailablePlaces
function inAvailablePlaces.jsx
- send the updated selection of places to the backend in
App.jsx
- send the request in
handleSelectPlace
after updating theuserPlaces
state with help of thefetch
function - define a new
updateUserPlaces
function in thehttp.js
helper file & call it inApp.jsx
- pass the old
userPlaces
state & the newlyselectedPlaces
in front of it await
this function & decorate thehandleSelectPlace
function withasync
- use
try
/catch
to wrap thisupdateUserPlaces
function to catch potential errors
- send the request in
- select a place in the application & check the new data inserted in
data/user-places.json
- 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
- manage a new
- in
App.jsx
, turn thehandleRemovePlace
function into anasync
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 theuseCallback
dependencies array - wrap this HTTP request with a
try
/catch
& handle the error
- fetch the places stored by the user in
App.jsx
with help ofuseEffect
to avoid an inifinite loop when you update the state - in
http.js
, define a new function namedfetchUserPlaces
- in
App.js
, call thefetchUserPlaces
function inside ofuseEffect
- 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